【问题标题】:SubSonic 3, build dynamic or expression at runtimeSubSonic 3,在运行时构建动态或表达式
【发布时间】:2010-10-29 07:25:17
【问题描述】:

我有一种情况,我必须根据用户选择动态构建 linq 查询。 如果我必须动态生成 sql,我可以这样做:

        var sb = new StringBuilder();
        sb.AppendLine("SELECT * FROM products p");
        sb.AppendLine("WHERE p.CategoryId > 5");

        // these variables are not static but choosen by the user
        var type1 = true;
        var type2 = true;
        var type3 = false;

        string type1expression = null;
        string type2expression = null;
        string type3expression = null;

        if (type1)
            type1expression = "p.productType1 = true";

        if (type2)
            type2expression = "p.productType2 = true";

        if (type3)
            type3expression = "p.productType3 = true";

        string orexpression = String.Empty;
        foreach(var expression in new List<string>
               {type1expression, type2expression, type3expression})
        {
            if (!String.IsNullOrEmpty(orexpression) &&
                    !String.IsNullOrEmpty(expression))
                orexpression += " OR ";

            orexpression += expression;
        }

        if (!String.IsNullOrEmpty(orexpression))
        {
            sb.AppendLine("AND (");
            sb.AppendLine(orexpression);
            sb.AppendLine(")");
        }

        // result:

       // SELECT * FROM products p
       // WHERE p.CategoryId > 5
       // AND (
       // p.productType1 = true OR p.productType2 = true
       // )

现在我需要以同样的方式创建一个 linq 查询。

这适用于亚音速

var result = from p in db.products
             where p.productType1 == true || p.productType2 == true
             select p;

我使用 PredicateBuilder http://www.albahari.com/nutshell/predicatebuilder.aspx 进行了尝试,但这会引发 subsonic 异常。

var query = from p in db.products
            select p;

var inner = PredicateBuilder.False<product>();
inner = inner.Or(p => p.productType1 == true);
inner = inner.Or(p => p.productType2 == true);

var result = query.Where(inner);

抛出的异常:NotSupportedException: The member 'productType1' is not supportedSubSonic.DataProviders.MySQL.MySqlFormatter.VisitMemberAccess

任何人都知道如何让这个查询工作:

【问题讨论】:

  • 嗨 SchlaWeiner,我也按照您的方式构建查询。我通常用它来过滤、排序和分页。你有一个优雅的方法来做到这一点吗?
  • 我添加了一个例子。只需按照 Devart 提供的链接,下载示例项目并将 Dynamic.cs 文件添加到您的项目中。您必须将using System.Linq.Dynamic 添加到您的代码中才能使用该库。

标签: c# mysql linq subsonic predicatebuilder


【解决方案1】:

也许Dynamic LINQ 会有所帮助?

【讨论】:

  • 是的,这行得通。我想那里有一个更静态类型的解决方案,我没有找到 atm。但是动态linq源码确实很强大。谢谢。
【解决方案2】:

根据 geocine 的要求,这是一个使用示例。 它需要动态 Linq。

        var productTypes = new int[] {1,2,3,4};
        var query = from p in db.products
                    select p;

        if (productTypes.Contains(1))
            query.Add("productType1 = @0");

        if (productTypes.Contains(2))
            query.Add("productType2 = @0");

        if (productTypes.Contains(3))
            query.Add("productType3 = @0");

        if (productTypes.Contains(4))
            query.Add("productType4 = @0");

        if (productTypes.Count > 0)
        {
            string result = String.Join(" OR ", productTypes);
            query = query.Where("(" + result + ")", true);
        }

        var result = from p in query
                     select new {Id = p.ProductId, Name = p.ProductName };

它看起来很糟糕,但它确实有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多