LINQ to SQL Where Clause Optional Criteria

Written by rsolberg on. Posted in All, Technology

Often times you may allow the users to query your system and you give them the choice of which fields to use in their query. This entry covers how to use optional where criteria within your query and avoid having to write 12 versions of the same query with different where clauses! Let’s take a real example of what I’m talking about… Suppose you have the following basic LINQ query.
  • Product Number – Comes from another table that can be joined to ItemsHeaders.
  • PO Number – a field within the ItemsHeaders table.
  • Order Number – Similar to PO #, just different column.
  • Product Status – Apply if the user selected this from a drop down.In order to accomplish this task, you need to create a couple of extension methods that allow you to apply a where filter if a condition is met.
    New Query!
    I must give special thanks to Andrew Robinson for assisting me with this code and functionality.
  • Be Sociable, Share!

    Tags: , ,

    Trackback from your site.

    Leave a comment

    © Copyright RSolberg, 2003 - 2014. All Rights Reserved