[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Linq query mods. at run time

Posted on 2014-01-23
6
Medium Priority
?
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 11

Accepted Solution

by:
Angelp1ay earned 2000 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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 this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

656 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