Solved

Linq query mods. at run time

Posted on 2014-01-23
6
196 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

679 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