为什么迭代组与迭代linq中的集合不同

山姆

在下面的查询中,很容易看出我们基本上是如何遍历sampleOrders并评估每个订单的,即可以访问ord的属性:

var o = (
            from ord in sampleOrders
            select ord.Quantity

在下面的查询中,我们将按产品分组的订单分为orderGroups。orderGroups从概念上讲是一个“列表列表”,其中每个列表都有一个键和一个包含关联订单的属性。前面的描述当然很松散-关键是,如果我迭代该集合,我希望会收到一个具有key属性和包含订单的属性的对象。此行为似乎与上面的第一个查询一致。但实际上每个组都是一个命令。通过迭代组列表来显示它,而我们绕过了该组本身,实际上是迭代了每个顺序。
为什么此分组行为与上面的第一个查询中的行为不同?

var o = (
            from ord in sampleOrders
            group ord by ord.Product into orderGroups
            from grp in orderGroups
            select grp  // grp is an order, not a group of orders

顺便说一句,请注意,我不是在问如何访问组.....我的问题与概念和语法有关。这是访问每个组的方法。...我不是在问如何执行此操作:

var o = (
            from ord in sampleOrders
            group ord by ord.Product into orderGroups
            select new {orderGroups.Key, SumQty=orderGroups.Sum(x => x.Quantity)}

编辑:每个请求的工作示例

public static void LinqTest()
    { 
        List<Order> sampleOrders = new List<Order>()
        {
            new Order{Product="A", Quantity = 3},
            new Order{Product="A", Quantity = 4},
            new Order{Product="B", Quantity = 5},
            new Order{Product="C", Quantity = 6},
        };


        var orders =
            from ord in sampleOrders
            group ord by ord.Product into orderGroups
            select orderGroups;

        foreach (var grp in orders)      // here is the outer list
        {
            Console.WriteLine("Product: " + grp.Key.ToString());

            // here is the inner list hence the term "list of lists" 
            // it is in fact a hierarchical object however this fact is irrelelvent to my question
            // 
            foreach (Order ord in grp)   
                Console.WriteLine("Qty: " + ord.Quantity);


        }

        Console.WriteLine();
        Console.WriteLine("following listing is grouped orders but we see each order not groups");

        var orders2 =
         from ord in sampleOrders
         group ord by ord.Product into orderGroups
         from grp in orderGroups
         select grp;

        foreach(Order ord2 in orders2)
            Console.WriteLine("Product: " + ord2.Product);  // each item returned is an order not a group of orders


        Console.ReadKey();
    }
}

class Order
{
    public string Product { get; set; }
    public int Quantity { get; set; }
}

编辑2:

这是澄清我的问题的另一种方法:在“ from listOfx中的x中”上方的第一个查询中,将为您提供一个对象x,该对象与listOfx组成的对象相同。

在组查询中,“来自listOfxGroups中的x”为您提供了一个与组成listOfxGroups的对象不同的对象。listOfxGroups实际上是一个组列表...但是,当我们对其进行迭代时,我们得到的x不是“ x的组”。

脱衣战士
        from ord in sampleOrders
        group ord by ord.Product into orderGroups
        from grp in orderGroups
        select grp

乍一看,可能会认为这转化为:

sampleOrders.GroupBy(ord => ord.Product)
    .Select(grp => grp); // grp is an `IGrouping<,>`

但实际上,它转换为:

sampleOrders.GroupBy(ord => ord.Product)
    .SelectMany(orderGroup => orderGroup); // orderGroup is an `IGrouping<,>`
    .Select(grp => grp); // grp is an Order

这就是LINQ语句的翻译方式:在初始from关键字之后,所有剩余的froms都会产生一个SelectMany。例如:

一个查询表达式,带有第二个from子句,后跟一个select子句

from x1 in e1
from x2 in e2
select v

被翻译成

( e1 ) . SelectMany( x1 => e2 , ( x1 , x2 ) => v )

C#5语言规范,第7.16.2.4节

from a in x
from b in a
select b

现在,in中的lambda表达式SelectMany必须返回一个IEnumerable<T>(它也这样做,因为IGrouping<TKey, TValue>它也是一个IEnumerable<TValue>),并且SelectMany将所有IEnumerable<T>s展平为一个big IEnumerable<T>,这意味着Order在这种情况下,其中的单个项是s。

更新

为了帮助澄清,请想象以下内容:

var orderGroups =              // orderGroups is plural: IEnumerable<IGrouping<,>>
    from ord in sampleOrders
    group ord by ord.Product;
var o =
    from orderGroup in orderGroups // orderGroup is singular: IGrouping<,>
    from item in orderGroup        // `item` instead of `grp`
    select item;

实际上,这是使用into子句生成的内容,但是具有不同的变量名。

var o = 
        from ord in sampleOrders
        group ord by ord.Product into orderGroup
        from item in orderGroup
        select item;               // item is an order, not a group of orders

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么迭代器存在于可迭代和集合接口的方法是什么?

为什么在迭代时添加到集合中和从集合中删除时会得到这么多迭代?

为什么在迭代时添加到集合中和从集合中删除时会得到这么多迭代?

为什么枚举不可迭代?

为什么在结构MUCH数组中连续迭代比在类数组中迭代更快?

D中的const集合的迭代

为什么循环在一次迭代中表现不同?

为什么要在LINQ扩展方法中的实际迭代器函数周围使用包装器?

为什么在迭代LINQ列表时可以编辑它?

为什么具有不同类型元素的集合不适用于迭代?

单元测试。为什么Mock在第二个循环迭代中返回不同的值?

LINQ中的可迭代析取

为什么继续使迭代停止?

为什么策略迭代比值迭代更快?

为什么排序被记录为采用可迭代而不是集合?

为什么在迭代器中返回的值类型不是集合?

LINQ:为什么数组和列表使用不同的迭代器

为什么Apex在这个集合上向后迭代?

为什么迭代循环太多?

在迭代中编辑集合吗?

Java:迭代集合的最佳方法是什么?

在 Frida 中迭代集合 - 创建迭代器

在 XAML 中迭代更多集合

为什么“NoneType”对象在 HackerRank 中不可迭代,但在 PyCharm 中不可迭代?

为什么Java中的HashSet需要迭代?

使用 LINQ 检查匹配属性的两个集合迭代的正确方法是什么?

为什么 obj 不可迭代

为什么我不能用 linq 迭代可以用 foreach 迭代的东西?

为什么这次迭代是随机的?