Nea*_*alR 4 linq entity-framework
我试图将查询结果返回到List对象,但是我通常使用的以下代码不起作用.Linq仍然相对较新,有人可以解释正确的语法/正在发生什么?如果我将数据类型更改productTraining
为var
...,这将有效
List<AgentProductTraining> productTraining = new List<AgentProductTraining>();
productTraining = from records in db.CourseToProduct
where records.CourseCode == course.CourseCode
select records;
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 13
Select()
并且Where()
将返回IQueryable<T>
,而不是List<T>
.你必须将它转换为List<T>
- 实际执行查询(而不仅仅是准备它).
您只需要ToList()
在查询结束时调用.例如:
// There's no need to declare the variable separately...
List<AgentProductTraining> productTraining = (from records in db.CourseToProduct
where records.CourseCode == course.CourseCode
select records).ToList();
Run Code Online (Sandbox Code Playgroud)
我个人不会使用查询表达式,当你所做的只是一个Where
子句时:
// Changed to var just for convenience - the type is still List<AgentProductTraining>
var productTraining = db.CourseToProduct
.Where(records => records.CourseCode == course.CourseCode)
.ToList();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
23531 次 |
最近记录: |