6

根据需要跳到“具体问题”。一些背景:

场景: 我有一组产品,其中包含一个填充有 DDL 的“向下钻取”过滤器(查询对象)。每个渐进式 DDL 选择将进一步限制产品列表以及为 DDL 留下的选项。例如,从工具中选择锤子会将产品尺寸限制为仅显示锤子尺寸。

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

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

具体问题:

我将如何将此查询重写为Dynamic Query?像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>. 感谢乔恩斯基特!投射对象后,您可以枚举集合并将结果输入到单独的列表中。

4

2 回答 2

4

我不确定如何使用 Query 语法(如上)执行此操作,但使用 Method 语法,我们可以使用 Expression<Func<,如下所示。此示例适用于 AdventureWorks SQL Server 数据库(Products 表),并允许您使用字符串指定要分组的列(在此示例中,我选择了大小)

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();
        }
    }
}
于 2010-04-03T21:02:55.040 回答
1

如果您查看C# Bits,作者将讨论如何使用动态数据创建级联过滤器。这听起来不像您在使用动态数据,但他确实有一个不错的表达式生成器,可能对您有所帮助。请参阅 BuildQueryBody,然后是关于 Iqueryable 的扩展方法的以下部分。

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

我希望这有帮助。

于 2010-04-01T14:00:36.543 回答