C# 如何将 IQueryable<T> 转换为 List<T>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1253421/
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
How to convert an IQueryable<T> to a List<T>?
提问by Prabu
Just learning LINQ and i've come to a newbie roadblock in my test project. Can you explain what i'm doing wrong?
刚刚学习 LINQ,我在我的测试项目中遇到了新手障碍。你能解释一下我做错了什么吗?
public List<ToDoListInfo> retrieveLists(int UserID)
{
//Integrate userid specification later - need to add listUser table first
IQueryable<ToDoListInfo> lists =
from l in db.ToDoLists
select new ToDoListInfo {
ListID = l.ListID,
ListName = l.ListName,
Order = l.Order,
Completed = l.Completed
};
return lists.ToList<ToDoListInfo>;
}
I'm getting an error saying the following:
我收到一条错误消息,内容如下:
Cannont convert method group 'ToList' to non-delegate type 'System.Collections.Generic.List' Do you intend to invoke the method?
无法将方法组“ToList”转换为非委托类型“System.Collections.Generic.List”您是否打算调用该方法?
采纳答案by Thomas Danecker
You just need parantheses:
你只需要括号:
lists.ToList<ToDoListInfo>();
lists.ToList<ToDoListInfo>();
Also, you do not have to declare the type parameter, i.e. you could use the following and let the type-system infer the type parameter:
此外,您不必声明类型参数,即您可以使用以下内容并让类型系统推断类型参数:
lists.ToList();
lists.ToList();
回答by Nathan W
You are just missing the closing brackets on ToList, should be:
您只是缺少 ToList 上的右括号,应该是:
ToList();
or
或者
ToList<ToDoListInfo>();