C# 如何将此Linq SQL编写为动态查询(使用字符串)?

C# 如何将此Linq SQL编写为动态查询(使用字符串)?,c#,linq,linq-to-sql,repository,dynamicquery,C#,Linq,Linq To Sql,Repository,Dynamicquery,根据需要跳到“特定问题”。一些背景: 场景:我有一组产品,其中有一个“向下钻取”过滤器(查询对象),填充了DDL。每个渐进式DDL选择将进一步限制产品列表以及DDL的剩余选项。例如,从工具中选择锤子会将产品尺寸限制为仅显示锤子尺寸 当前设置:我创建了一个查询对象,将其发送到存储库,并将每个选项提供给SQL“表值函数”,其中空值表示“获取所有产品” 我认为这是一个很好的努力,但远远不能接受DDD。我希望避免在SQL中进行任何“编程”,希望使用存储库完成所有工作。请就这一议题发表意见 具体问题: .

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

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

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

我认为这是一个很好的努力,但远远不能接受DDD。我希望避免在SQL中进行任何“编程”,希望使用存储库完成所有工作。请就这一议题发表意见

具体问题:

.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
如何将此查询重写为一个查询?链接到类似的东西会非常棒,但是有一个动态的查询范围。我真的想把引号“”中的字段传递给这个方法,我想要一个选项列表,以及有多少产品具有该选项

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
到目前为止,我所学到的是非常棒的,但仍然在寻找答案。我想最终,像这样完全随机的研究会占上风。哈哈

编辑:

.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
Jon Skeet有一个奇妙的想法:将我需要的内容转换为
I分组
。多亏了乔恩·斯基特!强制转换对象后,您可以枚举集合并将结果输入到单独的列表中。

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

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


我希望这有帮助

我不知道如何使用查询语法(如上所述)实现这一点,但是使用方法语法,我们可以使用一个表达式new{ Key=group.Key, Count=group.Count() }); foreach(结果中的var结果) WriteLine(“{0}:{1}”,result.Key,result.Count); } Console.ReadKey(); } } }
您需要完整的动态查询吗?你的过滤条件(Where子句)不是唯一动态的方面吗?我相信我需要一个完整的DQ。在不为每个要筛选的字段复制此代码的情况下,我需要将GroupBy字段指定为字符串。只有在返回“it”时才能转换为IGroupping。当您使用.Select(“new(Key,it as Product,count()as count)”)时,如何转换此属性????