Solved

Linq query mods. at run time

Posted on 2014-01-23
6
194 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
ID: 39803834
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
ID: 39803860
what do I do if I want the where clause excluded entirely?
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39803909
That is taken into account in the above. If ShowOneTeamOnly is false it ignores the rest of the where.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

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

Thanks
0
 

Author Closing Comment

by:petel2k
ID: 39803951
Thanks
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39803962
Welcome :) Have a good day!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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…

786 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