score:2

As mentioned in the comments above you can do this with expression trees. It will do what you need to do, but it may not be completely feasible, depending upon the size of your dataset; if you do expression trees you are doing the filtering in memory which may cause performance issues if you have large datasets.

Another option is to do the filtering in a stored procedure. Using SQL Server, for instance, you can default your parameters to NULL like this:

@Authors VARCHAR(50) = NULL,
@Genre VARCHAR(50) = NULL,
@BookId VARCHAR(50) = NULL,
@PublishedDate VARCHAR(50) = NULL,
@Title VARCHAR(50) = NULL

And then filter in your where clause:

WHERE   (Authors LIKE @Authors OR @Authors IS NULL)
AND     (Genre LIKE @Genre OR @Genre IS NULL)
AND     (BookId LIKE @BookId OR @BookId IS NULL)
AND     (PublishedDate LIKE @PublishedDate OR @PublishedDate IS NULL)
AND     (Title LIKE @Title OR @Title IS NULL)

That makes for some ugly SQL, and you should probably clean up the data types, but you get the idea. Going the SQL route is not really sustainable for a large number of search parameters.

If you decide to go the expression tree route, I have a demo project I wrote for a presentation that you might be able to use as a starting point:

Expression Trees

Hope that helps


Related Query

More Query from same tag