LINQ的 - 混乱的SelectMany [英] Linq - SelectMany Confusion

查看:112
本文介绍了LINQ的 - 混乱的SelectMany的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我从的SelectMany的文档理解,人们可以用它制作的一对多关系(扁平)序列。

From what I understand from the documentation of SelectMany, one could use it to produce a (flattened) sequence of a 1-many relationship.

我有以下类

  public class Customer
  {
    public int Id { get; set; }
    public string Name { get; set; }
  }

  class Order
  {
    public int Id { get; set; }
    public int CustomerId { get; set; }
    public string Description { get; set; }
  }



然后我尝试使用像这样的查询表达式语法

I then try to use them using the query expression syntax like so

  var customers = new Customer[]
  {
    new Customer() { Id=1, Name ="A"},
    new Customer() { Id=2, Name ="B"},
    new Customer() { Id=3, Name ="C"}
  };

  var orders = new Order[]
  {
    new Order { Id=1, CustomerId=1, Description="Order 1"},
    new Order { Id=2, CustomerId=1, Description="Order 2"},
    new Order { Id=3, CustomerId=1, Description="Order 3"},
    new Order { Id=4, CustomerId=1, Description="Order 4"},
    new Order { Id=5, CustomerId=2, Description="Order 5"},
    new Order { Id=6, CustomerId=2, Description="Order 6"},
    new Order { Id=7, CustomerId=3, Description="Order 7"},
    new Order { Id=8, CustomerId=3, Description="Order 8"},
    new Order { Id=9, CustomerId=3, Description="Order 9"}
  };

  var customerOrders = from c in customers
                       from o in orders
                       where o.CustomerId == c.Id
                       select new 
                              { 
                                 CustomerId = c.Id
                                 , OrderDescription = o.Description 
                              };

  foreach (var item in customerOrders)
    Console.WriteLine(item.CustomerId + ": " + item.OrderDescription);

这给了我所需要的。

1: Order 1
1: Order 2
1: Order 3
1: Order 4
2: Order 5
2: Order 6
3: Order 7
3: Order 8
3: Order 9

我假定这将转换为使用未使用查询表达式语法时,方法的SelectMany

I assume this translates to using the SelectMany method when not using the query expression syntax?

无论哪种方式,我想换我的头用身边的SelectMany。所以,即使我上面的查询并没有转化到的SelectMany,给出的两个类和模拟数据,可能有人为我提供与使用的SelectMany LINQ查询?

Either ways, I'm trying to wrap my head around using SelectMany. So even if my above query does not translate to SelectMany, given the two classes and mock data, could someone provide me with a linq query that uses SelectMany?

推荐答案

下面是一个使用查询的SelectMany ,你的例子后正好建模。 !同样的输出

Here is your query using SelectMany, modeled exactly after your example. Same output!

        var customerOrders2 = customers.SelectMany(
            c => orders.Where(o => o.CustomerId == c.Id),
            (c, o) => new { CustomerId = c.Id, OrderDescription = o.Description });



第一个参数每一个客户映射到订单的集合(完全analagous给你',其中'条款已经有)。

The first argument maps each customer to a collection of orders (completely analagous to the 'where' clause you already have).

第二个参数将每个匹配对{(C1,01),(C1,O2)。(C3,O9)}到一个新的类型,这是我所做的与您的例子

The second argument transforms each matched pair {(c1, o1), (c1, o2) .. (c3, o9)} into a new type, which I've made the same as your example.

所以:


  • ARG1基地集合中的每个元素映射到另一个集合。

  • ARG2(可选)将每个对进入一个新的类型

返回的集合是平的像你在你原来的例子预期。

The resulting collection is flat like you'd expect in your original example.

如果你省略第二个参数,你将结束与所有订单匹配到一个客户的集合。这将会是这一点,订单对象的集合平

If you were to omit the second argument, you would end up with a collection of all orders the match up to a customer. It'd be just that, a flat collection of Order objects.

使用它需要大量的时间来的到,我仍然有麻烦包装我的头周围的时候。 (

Using it takes a lot of getting used to, I still have trouble wrapping my head around it sometimes. :(

这篇关于LINQ的 - 混乱的SelectMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆