相关疑难解决方法(0)

将IEnumerable <T>转换为List <T>

我想知道是否可以将IEnumerable转换为List.除了将每个项目复制到列表中之外,还有什么方法可以做到吗?

c# ienumerable list

32
推荐指数
3
解决办法
7万
查看次数

Linq IEnumerable <IGrouping <string,Class >>返回List <Class>

我怎样才能将以下陈述改回来 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)

c# linq

6
推荐指数
1
解决办法
8804
查看次数

如何在C#中将System.Collection.IEnumerable转换为List &lt;T&gt;?

如何System.Collection.IEnumerable在C#中转换为列表?实际上,我正在执行一个存储过程,该过程给了我ResultSets,System.Collection.IEnumerable并且我想将该结果集转换为c#List<User>

注意我不想使用任何循环。有没有一种类型转换的方法!

c# sql ado.net

5
推荐指数
1
解决办法
3962
查看次数

从IEnumerable到List的无效转换

我正在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)

c# casting type-conversion

1
推荐指数
1
解决办法
662
查看次数

标签 统计

c# ×4

ado.net ×1

casting ×1

ienumerable ×1

linq ×1

list ×1

sql ×1

type-conversion ×1