Solved

Linq query mods. at run time

Posted on 2014-01-23
6
191 Views
Last Modified: 2016-02-10
I have a query below, note the where clause in bold. In some cases I don't want to include the where clause (omit it)  and other cases I do. Is there a way to code it in such a way so that in some cases it looks like the query beneath this one, without repeating the query?

ie.. if (ShowOneTeamOnly) .. then show with 'where', if not, then no 'where' to be used, but embed the logic inside the query.  Thanks

var ANames = (from g in AllData
                              where c.dbTeam.Trim() == sTeam.Trim() &&  c.dbGrp == sGrp
                              group g by new
                              {
                                  g.dbName
                              } into g
                              select new
                              {
                                  Agentname = g.Key.dbName

                              }).ToList();


sometimes I want it to look as this, without the where..

var ANames = (from g in AllData
                              group g by new
                              {
                                  g.dbName
                              } into g
                              select new
                              {
                                  Agentname = g.Key.dbName

                              }).ToList();
0
Comment
Question by:petel2k
  • 3
  • 3
6 Comments
 
LVL 11

Accepted Solution

by:
Angelp1ay earned 500 total points
Comment Utility
You could include the condition in the where:
var ANames = (from g in AllData
            where !ShowOneTeamOnly || (c.dbTeam.Trim() == sTeam.Trim() &&  c.dbGrp == sGrp)
            group g by new {g.dbName} into g
            select new {Agentname = g.Key.dbName}).ToList();

Open in new window

I would assume this to be optimised well by LINQ / SQL.
0
 

Author Comment

by:petel2k
Comment Utility
what do I do if I want the where clause excluded entirely?
0
 
LVL 11

Expert Comment

by:Angelp1ay
Comment Utility
That is taken into account in the above. If ShowOneTeamOnly is false it ignores the rest of the where.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:petel2k
Comment Utility
I see what you are doing,, I'll just expression everything in..

Thanks
0
 

Author Closing Comment

by:petel2k
Comment Utility
Thanks
0
 
LVL 11

Expert Comment

by:Angelp1ay
Comment Utility
Welcome :) Have a good day!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now