选择许多Linq [英] SelectMany Linq

查看:40
本文介绍了选择许多Linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释这个SelectMany语法

Could anyone explain this SelectMany Syntax

List<string> animals = new List<string>() { "cat", "dog", "donkey" };
List<int> number = new List<int>() { 10, 20 };
var mix=number.SelectMany(num => animals, (n, a) => new { n, a });

我不熟悉该语法,因为我习惯于将其用于拼合(拼合列表中的列表)

I am not familiar with that syntax because I am used to use it for the purposes of flattening (to flatten a list inside list)

 public class x
 {
     public List<z> Zs{get; set;}
     public int Y { get; set; }
 }
 public class z
 {
     public int Y { get; set; }
 }
    List<x> Xs = new List<x>();
    Xs.Add(new x() { Zs = new List<z>() { new z() { Y = 15 } } });
    Xs.Add(new x() { Zs = new List<z>() { new z() { Y = 17 } } });
    Xs.Add(new x() { Zs = new List<z>() { new z() { Y = 19 } } });
    Xs.Add(new x() { Zs = new List<z>() { new z() { Y = 25 } } });
    Xs.Add(new x() { Zs = new List<z>() { new z() { Y = 50 } } });

Xs.SelectMany(x => x.Zs).Sum(z => z.Y);

推荐答案

从Microsoft

Check the reference source from Microsoft here

以下是正在利用的SelectMany重载

        public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector) 
        {
            if (source == null) throw Error.ArgumentNull("source");
            if (collectionSelector == null) throw Error.ArgumentNull("collectionSelector");
            if (resultSelector == null) throw Error.ArgumentNull("resultSelector");
            return SelectManyIterator<TSource, TCollection, TResult>(source, collectionSelector, resultSelector);
        }

        static IEnumerable<TResult> SelectManyIterator<TSource, TCollection, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector) 
        {
            foreach (TSource element in source) 
            {
                foreach (TCollection subElement in collectionSelector(element)) 
                {
                    yield return resultSelector(element, subElement);
                }
            }
        }

其工作方式(这也是数据拼合的形式)

How its working (this is also form of data flattening)

  1. 类似于/foreach循环的同心圆,遍历两个集合并创建一个组合列表,其复杂度为O(N ^ 2)
  2. 首先,我们将数字列表提供为IEnumerable<TSource>
  3. 添加一个功能,其中每个number我们都提供animal列表
  4. 最后只是将结果作为组合的匿名集合来获取,其中为每个数字添加了一个动物,因此结果就像

  1. Similar to a concentric for / foreach loops, traversing trough two collections and creating a combined list, which has the complexity of O(N^2)
  2. First we supply the number list as the IEnumerable<TSource>
  3. Add a Func, where for each number we supply the animal list
  4. Finally just get the result as the combined anonymous collection, where for each number an animal is added and thus the result is like

  n    a

  10   cat 
  10   dog 
  10   donkey 
  20   cat 
  20   dog 
  20   donkey 

这篇关于选择许多Linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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