【发布时间】:2009-09-21 08:15:02
【问题描述】:
我已经在几个项目中使用了 linq2sql,但我决定是时候尝试一下 EF,因为它应该更强大、更好。有几件事真的很烦人。 其中之一是将结果投影到列表中,这在 l2sql 中效果很好,但在 EF 中效果不佳。
public class bo.Transaction
{
public long Id { get; set; }
public List<bo.Line> Lines { get; set; }
}
public class bo.Line
{
public int RowNo { get; set; }
public string Descripton{ get; set; }
public double Amount{ get; set; }
}
return from t in Db.Transaction.Include("Lines")
select new bo.Transaction {
Id = t.Id,
Lines = t.Lines.OrderBy(l => l.RowNo).ToList(),
};
但是,ToList() 调用失败,并显示消息“System.NotSupportedException:LINQ to Entities 无法识别方法 'List[bo.Line] ToListLine' 方法,并且此方法无法转换为存储表达式..”。
有什么办法可以解决这个问题?还是我只需要使用 ienumerable 而不是列表?
【问题讨论】:
-
一般来说,尽可能选择 IList 而不是 List。
标签: c# .net linq entity-framework