本文介绍了动态LINQ - 是否有.NET 4的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待使用LINQ的一些搜索程序和想有一些动态的where子句。因此,举例来说,如果用户想通过城市搜索或由国家搜索,我将有一个动态的LINQ其中<?>调用而不是创建两个强类型的LINQ表达式,然后使用基于用户希望如何寻找一个合适的

I'm looking to use LINQ for some searching routines and wanted to have some dynamic where clauses. So, for example, if a user wants to search by city or search by state, I would have a dynamic LINQ Where<> call instead of creating two strongly typed LINQ expressions and then using the appropriate one based on how the user wants to search.

所以我想做到这一点:

String criteria="p.City='Pittsburgh'";  //or "p.State='PA'"
personData.Where(criteria)

而不是

personData.Where(p => p.City ==匹兹堡);

personData.Where(p => p.State == PA);

我碰到一个的由Scott Guthrie的在Visual Studio 2008示例谈论动态LINQ。这似乎做我想做的,但我的问题是:

I came across a blog post by Scott Guthrie talking about Dynamic LINQ in the Visual Studio 2008 samples. This seems to do what I want, but my questions are:


  1. 是由微软支持这个样本库

  2. 斯科特·格思里的文章是关​​于VS2008(.NET 3.5)。是否有.NET 4是更好的选择?也许这在过去是使用.NET 4发布了完成同样的事情(或极为类似)?

提前感谢!

推荐答案

此功能将是非常不错的。类似的功能 ADO.net数据表存在。这将是对于 LinqToSql 以及真正有用的。当然,你会失去强类型检查,但多数民众赞成整点,你要动态搜索。如果处理异常正确我真的觉得它的一个特点值得拥有。

This feature would be really nice to have. A similar feature exists in ADO.net Datatables. It would be really helpful for LinqToSql as well. Sure you would lose strongly typed checking, but thats the whole point, you want dynamic searches. If you handle the exceptions properly I really think its a feature worth having.

您可以考虑加入一个功能请求以的的。该库已经存在,也许他们会考虑增加它的官方支持。如果确实进行功能要求确保您发布链接在这里,所以我们可以为它投票。 Microsoft连接具有类似于StackOverflow的投票系统。我已经提交了一些自己的和的。

You might consider adding a feature request to Microsoft Connect. The library already exists maybe they will consider adding official support for it. If you do make a feature request make sure you post a link here so we can vote for it. Microsoft Connect has voting system similar to stackoverflow. I have submitted a few myself LinqtoSql TableUpdate and VB.net Readonly Interfaces like C#.

我记得有一些麻烦与此库。我认为它有一些东西需要用静态方法。

I remember having some trouble with this library. I think it had something to do with static methods.

我发现它更好地发展,我需要表达。伊利亚Builuk 这本文演示自定义表达式。有关伊利亚的框架的好处它消除了做像的jqGrid排序操作时,很多样板代码。

I found it better to develop the expressions I needed. This article by Ilya Builuk demonstrates custom expressions. The nice thing about Ilya's framework it removes a lot of the boilerplate code when doing operations like sorting for jqGrid.

我发现学习表达的基本概念时非常有帮助

I found it extremely helpful when learning about the underlying concepts of expressions.

这个代码的好处是,它可以让你用点运营商干将。 Person.Age 或者,如果你想违反德米特你甚至可以做多的干将。

The nice thing about this code is that it allows you to use dot operators for getters. Person.Age or if you want to violate Demeter you can even do multiple getters.

该代码可以提高根据。我相信我加入 StartsWith 键,只允许其用于字符串操作,以及一些其他的搜索操作。 。不管它值得一看,它帮助我了解LINQ表达式很多

The code can be improved upon. I believe I added StartsWith and only allowed it for string operations as well as a few other search operations. Regardless its worth a look, it helped me understand linq expressions a lot.

public static IQueryable<T> Where<T>(this IQueryable<T> query, string column, object value, WhereOperation operation)
{
    if (string.IsNullOrEmpty(column))
        return query;

    ParameterExpression parameter = Expression.Parameter(query.ElementType, "p");

    MemberExpression memberAccess = null;
    foreach (var property in column.Split('.'))
        memberAccess = MemberExpression.Property
           (memberAccess ?? (parameter as Expression), property);

    //change param value type
    //necessary to getting bool from string
    ConstantExpression filter = Expression.Constant
        (
            Convert.ChangeType(value, memberAccess.Type)
        );

    //switch operation
    Expression condition = null;
    LambdaExpression lambda = null;
    switch (operation)
    {
        //equal ==
        case WhereOperation.Equal:
            condition = Expression.Equal(memberAccess, filter);
            lambda = Expression.Lambda(condition, parameter);
            break;
        //not equal !=
        case WhereOperation.NotEqual:
            condition = Expression.NotEqual(memberAccess, filter);
            lambda = Expression.Lambda(condition, parameter);
            break;
        //string.Contains()
        case WhereOperation.Contains:
            condition = Expression.Call(memberAccess,
                typeof(string).GetMethod("Contains"),
                Expression.Constant(value));
            lambda = Expression.Lambda(condition, parameter);
            break;
    }


    MethodCallExpression result = Expression.Call(
           typeof(Queryable), "Where",
           new[] { query.ElementType },
           query.Expression,
           lambda);

    return query.Provider.CreateQuery<T>(result);
}



WhereOperation枚举:

public enum WhereOperation { Equal, NotEqual, Contains }

这篇关于动态LINQ - 是否有.NET 4的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

查看更多