Solved

linq query with dynamic where's

Posted on 2013-11-13
1
269 Views
Last Modified: 2016-02-10
I am passing in the possibility of 7 parameters into a function  

public string GetFilter(string Var1, string Var2, string Var3, string Var4, string Var5, string Var6, string Var7)

The vars will not always have a value. The Var could be "". I want to build a linq query where my where clause uses the Var if it contains a value. What's the best way to accomplish this with the least amount of code overhead.
0
Comment
Question by:NickMalloy
1 Comment
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 400 total points
ID: 39646677
you can use composition to create your query. it is effective and very easy to read and maintain:

If Not String.IsNullOrEmpty(Var1) Then
     qry = qry.Where(Function(c) If(c.Family Is Nothing, "", c.Family) = Var1)
End If
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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)…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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