Entity Framework Code First & Search Criteria

2019-01-21 11:59发布

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?

2条回答
看我几分像从前
2楼-- · 2019-01-21 12:25

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.

查看更多
可以哭但决不认输i
3楼-- · 2019-01-21 12:29

John,

Your solution is absolutely awesome! But, just to share, I have been using this method above until I see your ideia.

var items = context.Items.Where(t => t.Title.Contains(keyword) && !String.IsNullOrEmpty(keyword));

So, it seems not to be the best solution for this, but for sure it is a way around.

查看更多
登录 后发表回答