我想知道是否可以将IEnumerable转换为List.除了将每个项目复制到列表中之外,还有什么方法可以做到吗?
我怎样才能将以下陈述改回来 List<DocumentData>
IEnumerable<IGrouping<string, DocumentData>> documents =
documentCollection.Select(d => d).GroupBy(g => g.FileName);
Run Code Online (Sandbox Code Playgroud)
目标是获得应该小于documentCollection的List.FileName包含重复项,因此我想确保我没有重复的名称.
我也试过以下但它仍然提供重复的文件名
documentCollection =
documentCollection.GroupBy(g => g.FileName).SelectMany(d => d).ToList();
Run Code Online (Sandbox Code Playgroud) 如何System.Collection.IEnumerable
在C#中转换为列表?实际上,我正在执行一个存储过程,该过程给了我ResultSets,System.Collection.IEnumerable
并且我想将该结果集转换为c#List<User>
。
注意我不想使用任何循环。有没有一种类型转换的方法!
我正在Unity3D项目中处理一个C#脚本,我正在尝试获取字符串列表并获得排列的2D列表.以下列方式使用此答案 GetPermutations()
:
List<string> ingredientList = new List<string>(new string[] { "ingredient1", "ingredient2", "ingredient3" });
List<List<string>> permutationLists = GetPermutations(ingredientList, ingredientList.Count);
Run Code Online (Sandbox Code Playgroud)
但它会引发隐式转换错误:
IEnumerable<IEnumerable<string>> to List<List<string>> ... An explicit conversion exists (are you missing a cast)?
所以我看了几个地方,比如这里,并提出了以下修改:
List<List<string>> permutationLists = GetPermutations(ingredientList, ingredientList.Count).Cast<List<string>>().ToList();
Run Code Online (Sandbox Code Playgroud)
但它在运行时中断,在内部处理,并允许它继续而不指示失败 - 可能是因为它在Unity3D中运行.这是我在停止调试脚本后在Unity3D中看到的内容:
InvalidCastException: Cannot cast from source type to destination type.
System.Linq.Enumerable+<CreateCastIterator>c__Iterator0`1[System.Collections.Generic.List`1[System.String]].MoveNext ()
System.Collections.Generic.List`1[System.Collections.Generic.List`1[System.String]].AddEnumerable (IEnumerable`1 enumerable) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:128)
System.Collections.Generic.List`1[System.Collections.Generic.List`1[System.String]]..ctor (IEnumerable`1 collection) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:65)
System.Linq.Enumerable.ToList[List`1] (IEnumerable`1 source)
Run Code Online (Sandbox Code Playgroud)
我认为这仍然是错误的,所以我也尝试了以下方法和更多我不记得的方法:
List<List<string>> permutationLists = GetPermutations(ingredientList, ingredientList.Count).Cast<List<List<string>>>();
List<List<string>> permutationLists = GetPermutations(ingredientList.AsEnumerable(), ingredientList.Count); …
Run Code Online (Sandbox Code Playgroud)