Dynamic Where With LINQ

It never seems to fail. At some point some one is going to want you to write something that will use one or more fields on a form to filter.  There are a number of ways to do this, but this little bit is focused on LINQ.  Why LINQ?  Because that is what I am doing at the moment, learning and really liking LINQ.

With LINQ it appears even easier than some of the traditional methods.  First you query as normal, part of that should include the fields you want to use in the where clause.  Once you have that you just filter that same query.

You simply check for the filter to have a selected or entered valid value.  Then you use the query.Where.  Inside that where you would do your filtering.  Here is an example

myQuery = myQuery.Where(b => b.statusID == nSelectedStatus);
Advertisements

About SheldonS

Web developer for over 15 years mainly with Microsoft technologies from classic ASP to .NET 4. Husband, father, and aspiring amateur photographer.

Posted on March 1, 2011, in Bits, LINQ and tagged . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: