views:

16

answers:

1

I have several columns that I'd like to search against. My code might not get passed anything for a given column to search against. So for the example below either lastname or firstname might be an empty string. Whenever I search against a column with an empty string it messes up the query. I've fixed the problem by checking to see if it's an empty string and not querying against it if it is. However I have a lot of columns to search against so that would be a lot of if/thens and make things messy. Any advice on how to best implement?

refinedresult = From x In theresult _
                        Where x.<lastname>.Value.ToLower.Contains(LastName.ToLower.Trim) Or _
                        x.<givenname>.Value.ToLower.Contains(FirstName.ToLower.Trim) Or               _
                        Select x
A: 

You can build an extension method on the type of x that performs the comparison and returns a bool. The method should take the reference string to compare to. If x is null simply return false from the extension method.

The ability to call an extension method on an reference which is null and handle the situation gracefully is really powerful.

Anders Abel
I don't have it working quite right but I think you put me on the right track. Thank you.
Cj Anderson