views:

16

answers:

1

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?

A: 

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 Turbomeca.EnginePowerCheck
{
    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.

John Gully