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);