0

我的 Linq to Entity 查询有问题。我试图返回这样的列表:

List<InvoiceReportItem> cancelResults = (List<InvoiceReportItem>)(from cr in model.InvoiceReportItems //join l in model.tblLicences on cr.LicenceAuto equals l.LicenceAuto where cr.Report.Id == id select cr);

但它给了我错误:

无法投射类型 >'System.Data.Objects.ObjectQuery 1[LicExpiryNotModel_EF41.Model.InvoiceReportItem]' to > type 'System.Collections.Generic.List1[LicExpiryNotModel_EF41.Model.InvoiceReportItem]' 的对象。

有谁知道为什么会出现这个错误?快把我逼疯了!:(

提前感谢尼尔

4

3 回答 3

3

使用ToList ()

List<InvoiceReportItem> cancelResults = 
 (from cr in model.InvoiceReportItems 
  join l in model.tblLicences on cr.LicenceAuto equals l.LicenceAuto where cr.Report.Id == id 
  select cr).ToList();
于 2012-07-27T10:47:29.670 回答
0

您不能将 anIQueryable转换为 a List,而是使用 上的.ToList()方法IQueryable,如下所示:

List<InvoiceReportItem> cancelResults = (from cr in model.InvoiceReportItems).ToList();
于 2012-07-27T10:48:51.310 回答
0

当您执行这样的查询时,LINQ 会延迟执行并返回 IQueryable。如果您愿意,这允许您在将来进一步查询(例如,添加额外的 where 子句)。无法将 IQueryable 泛型转换为列表。正如其他人所建议的那样,在代码上调用 ToList()

List<InvoiceReportItem> cancelResults = (List<InvoiceReportItem>)(from cr in model.InvoiceReportItems //join l in model.tblLicences on cr.LicenceAuto equals l.LicenceAuto where cr.Report.Id == id select cr).ToList();
于 2012-07-27T10:55:38.850 回答