根据需要跳到“具体问题”。一些背景:
场景: 我有一组产品,其中包含一个填充有 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>
. 感谢乔恩斯基特!投射对象后,您可以枚举集合并将结果输入到单独的列表中。