如何在 C# 中将 IEnumerable<T> 转换为 List<T>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 09:10:08  来源:igfitidea点击:

How can I convert IEnumerable<T> to List<T> in C#?

提问by Christian Hagelid

I am using LINQ to query a generic dictionary and then use the result as the datasource for my ListView (WebForms).

我正在使用 LINQ 查询通用字典,然后将结果用作我的 ListView (WebForms) 的数据源。

Simplified code:

简化代码:

Dictionary<Guid, Record> dict = GetAllRecords();
myListView.DataSource = dict.Values.Where(rec => rec.Name == "foo");
myListView.DataBind();

I thought that would work but in fact it throws a System.InvalidOperationException:

我认为这会起作用,但实际上它会抛出System.InvalidOperationException

ListView with id 'myListView' must have a data source that either implements ICollection or can perform data source paging if AllowPaging is true.

id 为“myListView”的 ListView 必须有一个数据源,该数据源要么实现 ICollection,要么在 AllowPaging 为 true 时可以执行数据源分页。

In order to get it working I have had to resort to the following:

为了让它工作,我不得不求助于以下几点:

Dictionary<Guid, Record> dict = GetAllRecords();
List<Record> searchResults = new List<Record>();

var matches = dict.Values.Where(rec => rec.Name == "foo");
foreach (Record rec in matches)
    searchResults.Add(rec);

myListView.DataSource = searchResults;
myListView.DataBind();

Is there a small gotcha in the first example to make it work?

在第一个示例中是否有一个小问题可以使其工作?

(Wasn't sure what to use as the question title for this one, feel free to edit to something more appropriate)

(不确定该用什么作为这个问题的标题,请随意编辑更合适的内容)

采纳答案by Matt Hamilton

Try this:

尝试这个:

var matches = dict.Values.Where(rec => rec.Name == "foo").ToList();

Be aware that that will essentially be creating a new list from the original Values collection, and so any changes to your dictionary won't automatically be reflected in your bound control.

请注意,这实际上是从原始 Values 集合创建一个新列表,因此对字典的任何更改都不会自动反映在绑定控件中。

回答by lomaxx

myListView.DataSource = (List<Record>) dict.Values.Where(rec => rec.Name == "foo");

回答by Jon Limjap

You might also try:

您也可以尝试:

var matches = new List<Record>(dict.Values.Where(rec => rec.Name == "foo"));

Basically generic collections are very difficult to cast directly, so you really have little choice but to create a new object.

基本上泛型集合很难直接转换,所以你真的别无选择,只能创建一个新对象。

回答by Keith

I tend to prefer using the new Linq syntax:

我倾向于使用新的 Linq 语法:

myListView.DataSource = (
    from rec in GetAllRecords().Values
    where rec.Name == "foo"
    select rec ).ToList();
myListView.DataBind();

Why are you getting a dictionary when you don't use the key? You're paying for that overhead.

当你不使用钥匙时,为什么你会得到一本字典?你正在为这笔开销买单。

回答by Elias MP

Just adding knowledge the next sentence doesn′t recover any data from de db. Just only create the query (for that it is iqueryable type). For launching this query you must to add .ToList() or .First() at the end.

只是添加知识下一句不会从 de db 恢复任何数据。只需创建查询(因为它是 iqueryable 类型)。要启动此查询,您必须在末尾添加 .ToList() 或 .First()。

dict.Values.Where(rec => rec.Name == "foo")