使用 EF 读取数据时,习惯性的写成
var quest = (from q in DbContext.TableName
select q).ToList();
今天需要在输出的结果上加上一个新的字段
所以写为
var quest = (from q in DbContext.TableName
select new {
q.a,
q.b,
新字段 = 0
}).ToList();
如果 这个“新字段”是一个固定值或对象。是没有问题的。
但如果 “ 新字段 ” 的内容是需要计算的。
且需要计算的结果来自其他对象的情况下
例如: 新字段 = getValue(q.a, 其他数据)
系统 就会 出错:This could potentially cause memory leak. Consider making the method static so that it does not capture constant in the instance.
网上查到:
注意到:Jack J Jun
的回答。
我自己的理解 是。根据 微软 EF 文档中的说明,
需要把代码修改为:
var TableName = DbContext.TableName.ToList();
var quest = (from q in TableName
select new {
q.a,
q.b,
getValue(q.a, 其他数据)
}).ToList()
这样就不会出错了!