development

Linq-SelectMany 혼란

big-blog 2020. 10. 5. 08:18
반응형

Linq-SelectMany 혼란


내가 SelectMany의 문서에서 이해 한 바에 따르면이를 사용하여 일대 다 관계의 (평탄화 된) 시퀀스를 생성 할 수 있습니다.

다음 수업이 있습니다

  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; }
  }

그런 다음 쿼리 식 구문을 사용하여 사용하려고합니다.

  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 메서드를 사용하는 것으로 해석한다고 가정합니다.

어느 쪽이든 SelectMany를 사용하여 머리를 감싸려고합니다. 따라서 위의 쿼리가 두 개의 클래스와 모의 데이터가 주어지면 SelectMany로 변환되지 않더라도 누군가 SelectMany를 사용하는 linq 쿼리를 제공 할 수 있습니까?


다음은을 사용하는 쿼리 SelectMany입니다. 동일한 출력!

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

첫 번째 인수는 각 고객을 주문 모음에 매핑합니다 (이미 가지고있는 'where'절과 완전히 분석 됨).

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.

So:

  • arg1 maps each element in the base collection to another collection.
  • arg2 (optional) transforms each pair into a new type

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. :(


SelectMany() works like Select, but with that extra feature of flattening a collection that is selected. It should be used whenever you want a projection of elements of sub-collections, and don't care about the sub-collection's containing element.

For example, let's say your domain looked like this:

public class Customer
  {
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Order> Orders { get; set; }
  }

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

To get the same list you wanted, your Linq would look something like this:

var customerOrders = Customers
                        .SelectMany(c=>c.Orders)
                        .Select(o=> new { CustomerId = o.Customer.Id, 
                                           OrderDescription = o.Description });

... which will produce the same result without needing the flat collection of Orders. The SelectMany takes each Customer's Orders collection and iterates through that to produce an IEnumerable<Order> from an IEnumerable<Customer>.


Though this is an old question, I thought I would improve the excellent answers a little:

SelectMany returns a list (which may be empty) for each element of the controlling list. Each element in these result lists are enumerated into the expressions' output sequence and so are concatenated into the result. Hence, a' list -> b' list[] -> concatenate -> b' list.

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using System.Diagnostics;
namespace Nop.Plugin.Misc.WebServices.Test
{
    [TestClass]
    public class TestBase
    {
        [TestMethod]
        public void TestMethod1()
        {  //See result in TestExplorer - test output 
            var a = new int[]{7,8};
            var b = new int[]
                    {12,23,343,6464,232,75676,213,1232,544,86,97867,43};
            Func<int, int, bool> numberHasDigit = 
                    (number
                     , digit) => 
                         ( number.ToString().Contains(digit.ToString()) );

            Debug.WriteLine("Unfiltered: All elements of 'b' for each element of 'a'");
            foreach(var l in a.SelectMany(aa => b))
                Debug.WriteLine(l);
            Debug.WriteLine(string.Empty);
            Debug.WriteLine("Filtered:" +  
            "All elements of 'b' for each element of 'a' filtered by the 'a' element");
            foreach(var l in a.SelectMany(aa => b.Where(bb => numberHasDigit(bb, aa))))
                Debug.WriteLine(l);
        }
    }
}

Here is another option using SelectMany

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

If you use the Entity Framework or LINQ to Sql and you have an association (relationship) between the entities, then you can do so:

var customerOrders = customers.SelectMany(
  c => c.orders
   .Select(p => new {CustomerId = c.Id, OrderDescription = p.Description}));

참고URL : https://stackoverflow.com/questions/5018361/linq-selectmany-confusion

반응형