Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Code First & Search Criteria

So I have a model created in Entity Framework 4 using the CTP4 code first features. This is all working well together.

I am attempting to add an advanced search feature to my application. This "advanced search" feature simply allows the users to enter multiple criteria to search by. For example:

Advanced Product Search

  • Name
  • Start Date
  • End Date

This would allow the user to search by the product name and also limit the results by the dates that they were created.

The problem is that I do not know how many of these fields will be used in any single search. How then can my Entity Framework query be constructed?

I have an example describing how to create a dynamic query for Entity Framework, however this does not seem to work for the POCO classes I created for Code First persistence.

What is the best way for to construct a query when the number of constraints are unknown?

like image 482
John Gully Avatar asked Oct 19 '10 16:10

John Gully


People also ask

What is Entity Framework core code first?

The Code First approach enables you to define an entity model in code, create a database from the model, and then add data to the database. MySQL Connector/NET is compatible with multiple versions of Entity Framework Core.

Which is better code first or database first in Entity Framework?

Versioning databases is hard, but with code first and code first migrations, it's much more effective. Because your database schema is fully based on your code models, by version controlling your source code you're helping to version your database.


1 Answers

So after some hours of work on this problem (and some help from our friend Google) I have found a workable solution to my problem. I created the following Linq expression extension:

using System;
using System.Linq;
using System.Linq.Expressions;

namespace MyCompany.MyApplication
{
    public static class LinqExtensions
    {
        public static IQueryable<TSource> WhereIf<TSource>(this IQueryable<TSource> source, bool condition, Expression<Func<TSource, bool>> predicate)
        {
            if (condition)
                return source.Where(predicate);
            else
                return source;
        }
    }
}

This extension allows for a Linq query to be created like this:

var products = context.Products.WhereIf(!String.IsNullOrEmpty(name), p => p.Name == name)
                               .WhereIf(startDate != null, p => p.CreatedDate >= startDate)
                               .WhereIf(endDate != null, p => p.CreatedDate <= endDate);

This allows each WhereIf statement to only affect the results if it meets the provided condition. The solution seems to work, but I'm always open to new ideas and/or constructive criticism.

like image 69
John Gully Avatar answered Oct 21 '22 04:10

John Gully