The scenario that came up today was this. A data entry application has the following fields, FirstName, LastName, and EmailAddress.
On this form those fields have a autocomplete lookup, on selection an event files that goes back to the server to see if the user exist. If one record exist it will auto fill all the other fields.
So where the trouble comes in is what happens if we want to fill those fields in, in a random fashion. Do we need to write complex business rules to support each of the options. No here is a non-optimized option
var ignoreFirstName = false; var ignoreLastName = false; var ignoreEmailAddress = false; if (person.FirstName != string.Empty) { ignoreFirstName = true; } if (person.LastName != string.Empty) { ignoreLastName = true; } if (person.EmailAddress != string.Empty) { ignoreEmailAddress = true; } var request = (from i in this.context1.People where (!ignoreFirstName || i.FirstName == person.FirstName) where (!ignoreLastName || i.LastName == person.LastName) where (!ignoreEmailAddress || i.EmailAddress == person.EmailAddress) select i).ToList();
What we are doing in this code sample is looking at the fields to see if the fields are empty. If they are we flag the ignore variable true or false. In the Linq statement we then setup the where clause to check the flag. If it is false, ignore the line.
Nice post. You might be interested in my use of an Expression Tree for dynamically querying IQueryable objects. Here is a link to my post: http://activeengine.net/2010/12/19/how-to-create-server-side-paging-for-datatables-net-with-asp-net/
Nice post…thx for saving my time..
nice post !