Solved

linq query with dynamic where's

Posted on 2013-11-13
1
271 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FInd Image Control Gridview 3 36
Amazon S3 Images with .Net 3 32
Adding where clause to entity statement c# 1 48
Import a excel sheet in a grid 2 9
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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