• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

Linq query mods. at run time

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
petel2k
Asked:
petel2k
  • 3
  • 3
1 Solution
 
Angelp1ayCommented:
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
 
petel2kAuthor Commented:
what do I do if I want the where clause excluded entirely?
0
 
Angelp1ayCommented:
That is taken into account in the above. If ShowOneTeamOnly is false it ignores the rest of the where.
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
petel2kAuthor Commented:
I see what you are doing,, I'll just expression everything in..

Thanks
0
 
petel2kAuthor Commented:
Thanks
0
 
Angelp1ayCommented:
Welcome :) Have a good day!
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now