Multiple search criteria and EF code first - Online Free Computer Tutorials.

'Software Development, Games Development, Mobile Development, iOS Development, Android Development, Window Phone Development. Dot Net, Window Services,WCF Services, Web Services, MVC, MySQL, SQL Server and Oracle Tutorials, Articles and their Resources

Tuesday, August 7, 2018

Multiple search criteria and EF code first

I run into the infamous advanced user search requirement that every database application user requests. There is an entity/model that the user need to search based on a combination of several criteria. In my case it is a single view in a database which is exposed using a read-only entity object with all relevant fields that are needed to display in a list. The user is able to search the records using one of the three criteria shown above. As for the user interface part of the app, it is a no-brainer since all that is required is a form post when he/she clicks search. The really annoying part is when handling the postback action. The first thing that comes to mind is to have a series of if… else blocks to handle the different cases. That really sucks! The good thing is there is a really neat hack that you can apply if you are using EF code first as your data access layer. By writing a simple Linq expression extension to handle the different cases. Here is how it works. First create a static class in your project and add this function to it: Code Snippet public static class SearchExtension { public static IQueryable WhereIf(this IQueryable source, bool condition, ExpressionFuncbool predicate) { if (condition) { return source.


I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.

Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.


This article is related to

Code First,EF,LINQ

No comments:

Post a Comment