子查询的GroupBy方法语法

时间:2016-02-23 17:24:10

标签: c# linq linq-to-sql

我有一个查询语法查询,我需要将其转换为方法语法,因为我需要在子查询的末尾添加.Take()运算符。这就是我所拥有的:

var TheQuery = (from col in MyDC.SomeTable
                ordery ....
                where.....
                group s by s.SomeColumnID into TheGroups
                from subgroup in TheGroups
                ....)

我无法将其转换为方法语法;这就是我想写的:

var TheQuery = MyDC.SomeTable
                .OrderByDescending(....
                .Where(.....
                .GroupBy(s => s.SomeColumnID into TheGroups) //here
                .TheGroups //here
                .Where(....

我确信这很容易,但我无法理解。如何将查询转换为方法语法,以便分组可以用于子查询?

在Shyju的尝试后编辑

这就是我现在所拥有的:

var TheQuery = MyDC.SomeTable
                     .OrderByDescending(s => s.ExpirationDate)
                     .Where(s => s.ExpirationDate < DateTime.UtcNow)
                     .Where(s => s.UserID == SomeParameter)
                     .GroupBy(s => s.BasketID, v => v, (k, v) => new SomeModel() 
                      {
                           BasketID = v.BasketID,
                           ExpirationDate = v.ExpirationDate,
                           SomeProp = v.SomeProp
                           ....
                      }).Take(1).ToList();

我按BasketID对所有元素进行分组,其元素的ExpirationDate小于UtcNow,我只想要每个篮子的最新元素和SomeProp属性(一个字节)等于2,3或者4。

2 个答案:

答案 0 :(得分:0)

如果您正在寻找对数据进行分组,这应该可行。

var grouped2 = MyDC.SomeTable
             .GroupBy(s => s.Name, v => v, (k, v) => new {k, v});

您可以在对结果进行分组时调用Take方法。

var groupedWithFiveItemsPerGroup = MyDC.SomeTable.GroupBy(s => s.Name, v => v, 
                               (k, v) => new {  Name = k, FiveItems= v.Take(5) });

如果要在分组数据上添加where子句,也可以这样做。

var groupedWithFiveItemsPerGroup = MyDC.SomeTable.GroupBy(s => s.Name, v => v, 
         (k, v) => new {  Name = k, FiveItems= v.Where(r=>r.SomeProperty=="Hi").Take(5) });

s.Name替换为您的媒体资源名称。

答案 1 :(得分:0)

您可以过滤每个组中的元素:

var TheQuery = MyDC.SomeTable
            .OrderByDescending(....
            .Where(.....
            .GroupBy(s => s.SomeColumnID)
            .Select(items => new { Key = items.Key, 
                                   Values = items.Where(i => i.SomeOtherColumnId == someId).Take(10) });

如果您只想在单个数组中获取每个组的过滤元素,则使用SelectMany()

var TheQuery = MyDC.SomeTable
            .OrderByDescending(....
            .Where(.....
            .GroupBy(s => s.SomeColumnID)
            .SelectMany(items => items.Where(i => i.SomeOtherColumnId == someId).Take(10));

编辑

如果您只需要满足条件的每个组的第一个/顶部元素:

var TheQuery = MyDC.SomeTable
            .Where(s => s.ExpirationDate < DateTime.UtcNow)
            .Where(s => s.UserID == SomeParameter)                     
            .Select(v => new SomeModel
            {
                BasketID = v.BasketID,
                ExpirationDate = v.ExpirationDate,
                SomeProp = v.SomeProp
                //....
            })
            .GroupBy(s => s.BasketID)
            .Select(items => items.Where(item => item.SomeProp == 2 ||
                                                 item.SomeProp == 3 ||
                                                 item.SomeProp == 4).OrderByDescending(item => item.ExpirationDate).First()).ToList();