views:

284

answers:

1

I'm looking to add an "advanced search" capability to my ASP.NET/SQL Server 2005 application. Ideally, I'd like it to be table driven. For example, if my schema changes with the addition of a new column to a table that I want to search, I'd like to have the UI reflect the addition of the new column as a searchable field. I can envision some control tables that contain the searchable fields, their types, associated lookups, etc.

These tables can be consulted when constructing the UI. My question lies with the best approach to create the dynamic SQL. One approach would be to construct a parameterized SQL statement myself. But I'm intrigued by Linq, and somehow I think that the System.Linq.Dynamic namespace -- and Linq Expression Trees -- might provide a more elegant/robust solution?

Hopefully any such solution would work with either Linq to Sql or Linq to Entities. Am I right to investigate this path, or are is this problem space not really one of the use cases for expression trees?

+1  A: 

You could use expression trees, by building up a tree that does a comparison of all the fields in the schema and then passing that expression into Linq2Sql to get back out the sql you want. But why bother? It's more programming effort, the code will be harder to understand, and there’s no benefit other than a theoretical ability to swap out data providers.

(Hey Howard) - indeed - working with strings in this case is going to be a lot simpler than w/ expression trees. On the other hand, if you were going to provide a small query language to your users, then expression trees would come in handy.

Scott Weinstein
Hey Scott (my alt.net friend... it's Howard Pinsley)... so are you suggesting building up a string representation of a parameterized query?
Decker