[英]Linq query conversion from Ilist<> to List<>
I receive IList<>
data and stores them like this: 我收到
IList<>
数据并像这样存储它们:
IList<Student> StudentList = DataKilde.Studenter();
Now I want to read them into a List<Student>
: 现在我想将它们读到
List<Student>
:
List<Student> studentWhere3 = StudentList.Where(x=> x.StudentId > 2 && x.StudentId < 8).ToList();
This is working...becoz I think .ToList
converts it to Ilist<>
to List<>
? 这正在工作...因为我认为
.ToList
将其转换为Ilist<>
到List<>
?
My problem is, when I am doing this: 我的问题是,当我这样做时:
List<Student> studentWhere5 = from s in StudentList
where s.StudentId==2
select s
I get a conversion error and have tried these: 我收到转换错误,并尝试了以下方法:
from s in (List<Student>)StudentList
from s in StudentList as List<Student>
But it's not working.. I don't get why ? 但这不起作用..我不知道为什么吗?
That's because select s
returns an IQueryable<Student>
, which is not implicitly nor explicitly convertable to a List<Student>
, because there is no inheritance between the two. 那是因为
select s
返回一个IQueryable<Student>
,它既不能隐式也不能显式转换为List<Student>
,因为两者之间没有继承。
You need to materialize the query result into a list: 您需要将查询结果物化到一个列表:
(from s in StudentList
where s.StudentId==2
select s).ToList();
This will take all elements from the source collection ( s
) and store them in a List<Student>
, which implements IList<Student>
. 这将需要从源集合(所有元素
s
)并将它们存储在一个List<Student>
,它实现IList<Student>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.