Solved

linq query with dynamic where's

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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
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)…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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