[英]How to convert an IQueryable<T> to a List<T>?
Just learning LINQ and i've come to a newbie roadblock in my test project. 刚学习LINQ,我在测试项目中遇到了新手障碍。 Can you explain what i'm doing wrong? 你能解释我在做什么错吗?
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”。是否要调用该方法?
You just need parantheses: 您只需要使用括号:
lists.ToList<ToDoListInfo>();
Also, you do not have to declare the type parameter, ie you could use the following and let the type-system infer the type parameter: 另外,您不必声明type参数,即可以使用以下内容并让type-system推断出type参数:
lists.ToList();
You are just missing the closing brackets on ToList, should be: 您只是缺少ToList的右括号,应为:
ToList();
or 要么
ToList<ToDoListInfo>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.