根据需要跳过“特定问题”。一些背景:

场景: 我有一组带有DDL填充的“钻取”过滤器(查询对象)的产品。每个渐进的DDL选择将进一步限制产品列表以及DDL剩下哪些选项。例如,选择锤子的工具限制产品尺寸仅显示锤子尺寸。

当前设置: 我创建了一个查询对象,将其发送到存储库,并将每个选项馈送到SQL“表值函数”,其中null值表示“获取所有产品”。

我认为这是一个很好的努力,但远离DDD可以接受。我想避免在SQL中进行任何“编程”,希望可以使用存储库来完成所有操作。对此主题的评论将不胜感激。

具体问题:

我如何将此查询重写为 动态查询?类似的链接 101 linq示例 会很棒,但是具有动态的查询范围。我真的想传递此方法,以“引号”字段“”我想要一个选项列表以及有多少产品具有该选项。

from   p in db.Products
group  p by p.ProductSize into g
select new Category { 
       PropertyType = g.Key,
       Count = g.Count() }

每个DDL选项将具有“选择(21)”,其中(21)是具有该属性的产品的数量。选择一个选项后,所有其他剩余的DDL将使用其余的选项和计数进行更新。

编辑:附加说明:

.OrderBy("it.City") // "it" refers to the entire record
.GroupBy("City", "new(City)") // This produces a unique list of City
.Select("it.Count()") //This gives a list of counts... getting closer
.Select("key") // Selects a list of unique City
.Select("new (key, count() as string)") // +1 to me LOL.  key is a row of group
.GroupBy("new (City, Manufacturer)", "City") // New = list of fields to group by
.GroupBy("City", "new (Manufacturer, Size)") // Second parameter is a projection

Product
.Where("ProductType == @0", "Maps")
.GroupBy("new(City)", "new ( null as string)")// Projection not available later?
.Select("new (key.City, it.count() as string)")// GroupBy new makes key an object

Product
.Where("ProductType == @0", "Maps")
.GroupBy("new(City)", "new ( null as string)")// Projection not available later?
.Select("new (key.City, it as object)")// the it object is the result of GroupBy

var a = Product
        .Where("ProductType == @0", "Maps")
        .GroupBy("@0", "it", "City") // This fails to group Product at all
        .Select("new ( Key, it as Product )"); // "it" is property cast though

到目前为止我学到的是 linqpad 很棒,但仍在寻找答案。最终,我想这样的完全随机的研究将占上风。哈哈。

编辑:

乔恩·斯基特(Jon Skeet IGrouping<string, Product>. 。感谢Jon Skeet!施放对象后,您可以在集合上进行枚举,并将结果输入到单独的列表中。

有帮助吗?

解决方案

我不确定如何使用查询语法(如上所述)执行此操作,但是使用方法语法,我们可以使用表达式

using System;
using System.Linq;
using System.Linq.Expressions;

namespace LinqResearch
{
    public class Program
    {
        [STAThread]
        static void Main()
        {
            string columnToGroupBy = "Size";

            // generate the dynamic Expression<Func<Product, string>>
            ParameterExpression p = Expression.Parameter(typeof(Product), "p");

            var selector = Expression.Lambda<Func<Product, string>>(
                Expression.Property(p, columnToGroupBy),
                p
            );

            using (LinqDataContext dataContext = new LinqDataContext())
            {
                /* using "selector" caluclated above which is automatically 
                compiled when the query runs */
                var results = dataContext
                    .Products
                    .GroupBy(selector)
                    .Select((group) => new { 
                        Key = group.Key, 
                        Count = group.Count()
                    });

                foreach(var result in results)
                    Console.WriteLine("{0}: {1}", result.Key, result.Count);
            }

            Console.ReadKey();
        }
    }
}

其他提示

如果你看一下 C#位, ,作者讨论了如何使用动态数据创建级联过滤器。听起来您并不是在使用动态数据,但是他确实有一个很好的表达构建器,可能对您有所帮助。请参阅buildquerybody,然后参见Iqueryable上的扩展方法的以下部分。

由于您没有使用动态数据,因此您需要处理无法访问“ MetaForegeNKeyColumn”对象,但我怀疑他的方法可能会帮助您解决问题。

我希望这有帮助。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top