开发者

Linq to Entities Complex Dynamic Search

开发者 https://www.devze.com 2023-04-13 07:25 出处:网络
We\'re using the Entity Framework (MySQL connector) and are creating a central Search facility on our web application.

We're using the Entity Framework (MySQL connector) and are creating a central Search facility on our web application.

This link is almost exactly what I need, aside from the fact that he's using pre-defined entiti开发者_开发百科es and properties. In our search scenario, we'll have a dynamic number of search terms and fields (ie: user chooses to search on surname, value and city, or provider and advisor).

Is it possible to achieve this kind of functionality with LINQ, so that we can leverage the deferred loading mechanism? I really wanted to avoid generating SQL strings, if possible. I looked at Dynamic LINQ with Expression Trees but couldn't get this to work (or this).


I know you indicated that you wanted to avoid generating SQL strings, but that is often the easiest way. (Much easier than custom Expression Trees). If you are doing this in EF, I recommend you check out Entity Sql which works against your conceptual model but allows for more dynamic querying options than LINQ. LINQ is really suited to compile time query rather than run time queries. You can read up on Entity SQL at http://msdn.microsoft.com/en-us/library/bb387145.aspx.


since last week, we have a similar problem to face, here is an idea i just had for it. thought i share it with you.

interface IPerson
{            
    DateTime BirthDay { get; set; }

    string City { get; set; }            

    string FirstName { get; set; }

    string LastName { get; set; }
}

interface IFilter { }

interface IPersonFilter : IFilter { }

class PersonFilter : IPersonFilter
{            
    public DateTime? BirthDay { get; set; }

    public string City { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }            
}

static IQueryable<TSource> ApplyFilter<TSource, TFilter>(IQueryable<TSource> source, TFilter filter) where TFilter : IFilter
{
    const BindingFlags bindingFlags = BindingFlags.Public|BindingFlags.Instance|BindingFlags.GetProperty;

    var retval = source;
    foreach (var filterProperty in filter.GetType().GetProperties(bindingFlags))
    {
        var elementParameter = Expression.Parameter(source.ElementType, "type");
        var elementProperty  = Expression.Property(elementParameter, filterProperty.Name);

        var value = filterProperty.GetGetMethod().Invoke(filter, null);
        if (value != null)
        {
            var constantValue = Expression.Constant(value, elementProperty.Type);
            var expression = Expression.Equal(elementProperty, constantValue);
            retval = retval.Where(Expression.Lambda<Func<TSource, bool>>(expression, elementParameter));
        }
    }

    return retval;
}

so the idea is, that you have a filter where the names of the properties of filter match the property names of the object you want to run the filter against. and if the value of the property is not null, i build a expression for it. For the simplicity i do build Expression.Equal expressions only, but i am thinking about extending it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号