Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Linq where clause as a variable

I am trying to make a LINQ statement where the where clause comes from a variable. For example:

string whereClause = address.zip == 23456; var x = from something in someList where whereClause; 

Is this possible? I cannot seem to get it to work.

thanks,

Update - my where clause is predefined and will be based on user input so I don't think this will work for me. Basically whereClause is not constructed in the method, it is a parameter of the method which does the LINQ. I didn't explain that well here is a better example:

public void doLnq(string whereClause) {    var x = from something in someList where whereClause;    dowork(x); } 

Update - Just to sum up some of the suggestions and centralize everything.

I cannot use a switch to generate the where clause because there are way to many possibilities.

The dynamic linq post that a few of you have posted does look promising but i am having trouble related the linq to sql example to my linq to objects problem.

and @sLaks after looking through msdn http://msdn.microsoft.com/en-us/library/bb353734.aspx I am having trouble figuring out where you meant to use AsQueryable

thanks,

like image 841
kds6253 Avatar asked Jan 03 '12 20:01

kds6253


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

You need to assembly an Expression<Func<T, bool>> and pass it to the Where() extension method:

Expression<Func<T, bool>> whereClause = a => a.zip == 23456; var x = frSomeList.Where(whereClause); 

EDIT: If you're using LINQ to Objects, remove the word Expression to create an ordinary delegate.

like image 103
SLaks Avatar answered Sep 22 '22 11:09

SLaks


This:

var query = from something in someList where whereClause; 

is shorthand for:

var query = someList.Where(something => whereClause); 

Assuming someList is an IEnumerable<Address>, Where refers to the Enumerable.Where Extension Method. This method expects a Func<Address, bool> which you can define as follows:

Func<Address, bool> whereClause = address => address.Zip == 23456; var query = someList.Where(whereClause); 
like image 30
dtb Avatar answered Sep 21 '22 11:09

dtb