Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SelectMany Linq_C#_Linq - Fatal编程技术网

C# SelectMany Linq

C# SelectMany Linq,c#,linq,C#,Linq,有人能解释一下这种语法吗 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 }); List animals=newlist()

有人能解释一下这种语法吗

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 });
List animals=newlist(){“猫”、“狗”、“驴”};
列表编号=新列表(){10,20};
var mix=number.SelectMany(num=>animals,(n,a)=>new{n,a});
我不熟悉该语法,因为我习惯于将其用于展平(展平列表中的列表)

公共类x
{
公共列表Zs{get;set;}
公共整数Y{get;set;}
}
公共类z
{
公共整数Y{get;set;}
}
List Xs=新列表();
Add(new x(){Zs=new List(){new z(){Y=15}}});
Add(new x(){Zs=new List(){new z(){Y=17}}});
Add(newx(){Zs=newlist(){newz(){Y=19}}});
Add(new x(){Zs=new List(){new z(){Y=25}}});
Add(newx(){Zs=newlist(){newz(){Y=50}}});
x.SelectMany(x=>x.Zs).Sum(z=>z.Y);

检查来自Microsoft的参考源

以下是正在使用的
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);
                }
            }
        }

请澄清你需要帮助的部分。如果它写为
var mix=from n in number from a in animals,请选择new{n,a}这对你更有意义吗?@Enigmativity谢谢,是的sense@user3260672-这正是
number.SelectMany(num=>animals,(n,a)=>new{n,a})的LINQ形式。它们可以互换。
        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);
                }
            }
        }
  n    a

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