Querying using Linq and an embedded Criteria Class

Querying using Linq and an embedded Criteria Class

Old forum URL: forums.lhotka.net/forums/t/4981.aspx


justin.fisher posted on Monday, June 16, 2008

I'm relatively new to LINQ to SQL and I am a little confused on querying a datacontext for multiple criteria.  For instance I have a contact object that has fields for all the familar information first name, last name, title, etc.

I was hoping to have a dataportal_fetch() method that could handle querying for any combination of query criteria.  I began checking string.IsNullOrEmpty on each criteria property then running a query against the contact's datacontext but that is getting quickly out of hand.

In raw SQL I could just build the required query string then execute that and process a datareader to extract the results.  I did a quick search on LINQ to SQL predicates and came up with some hits that look like an entire evenings worth of reading.

I'm sure that this has been tackled by someone reading this forum and was hoping someone has some existing CSLA specific information they could point me to.

cliffordru replied on Monday, June 16, 2008

You have a few options:

  1. Write an initial LINQ query that is the least specific.  Follow this query with if statements based on your criteria values, building new LINQ statements based on the criteria.  This allows you to use LINQ but can quickly become a maintenance issue/violation of the DRY principle.
  2. Use the Dynamic Query Library.  These are extension methods that allow you to use string expressions instead of code expressions.  You can dynamically gen your strings, then pass to the methods.
  3. ADO.NET...if using dynamic sql or a stored procedure looks to be the cleanest solution, use it, you can use a mixture of LINQ and ADO.NET for your data access.

Hope that helps.

 

cliffordru replied on Monday, June 16, 2008

One more item to note, you may want to revisit the responsibility of your object, if it is trying to do too much (retrieve different info based on different scenarios/use cases) you may want to consider breaking out the responsibilities across multiple objects.

justin.fisher replied on Tuesday, June 17, 2008

I appreciate the feedback Cliff.  It turns out that after revisiting some of the requirements with the users the filtering was much simpler than I had anticipated.  Things are much simpler now, since I don't have to worry about combining search criteria.  Much easier to search on last name than last name and any number of other search criteria.

I did get a chance to look at some of the dynamic LINQ to SQL posts, I wasn't aware that these were extensions at first.   I was a little hesitant to thrown another new element into the development mix for now.  Especially since this is my first CSLA project so there is already a pretty steep learning curve. If I did have to implement this I would probably have opted for executing dynamic sql against the databae using ADO.net.


Copyright (c) Marimer LLC