为什么 Linq 需要一个“只读”设置器?对象属性?
我正在使用 Linq DataContext.ExecuteQuery("some sql statements") 来填充对象列表
var incomes = db.ExecuteQuery<IncomeAggregate>(sqlIncomeStatement(TimeUnit));
IncomeAggregate
是我用来保存此查询记录结果的对象。
该对象的属性之一是 YQM:
public int Year { get; set; }
public int Quarter { get; set; }
public int Month { get; set; }
public string YQM
{
get { return string.Format("Y{0}-Q{1}-M{2}", Year, Quarter, Month); }
}
... more properties
一切都编译正常,但是当它执行 Linq 时,我收到以下错误:
无法为成员“YQM”赋值。它没有定义 setter。
但显然,我不想“设置”它。 Y、Q 和 M 由对数据库的查询提供。查询未提供 YQM。 我需要以某种方式改变我的对象的定义吗? (我刚刚开始使用 Linq,并且仍在加快速度,所以它可能非常简单)
I'm using a Linq DataContext.ExecuteQuery("some sql statement") to populate a list of objects
var incomes = db.ExecuteQuery<IncomeAggregate>(sqlIncomeStatement(TimeUnit));
The IncomeAggregate
is an object I made to hold the result of the records of this query.
One of the properties of this object is YQM:
public int Year { get; set; }
public int Quarter { get; set; }
public int Month { get; set; }
public string YQM
{
get { return string.Format("Y{0}-Q{1}-M{2}", Year, Quarter, Month); }
}
... more properties
Everything compiles OK, but when it executes the Linq I get the following error:
Cannot assign value to member 'YQM'. It does not define a setter.
But clearly, I don't want to 'set' it.
Y, Q and M are provided by the query to the database. YQM is NOT provided by the query.
Do I need to change the definition of my object somehow?
(I've just started using Linq and I'm still getting up to speed, so it could be very simple)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我最终只是将设置器设为私有
似乎有效。
Well, I finally wound up just making the setter private
Seems to work.
Linq 假设该对象的属性是从数据库加载的值,并且显然它无法设置
YQM
属性,因为它没有设置器。尝试使 YQM 成为一种方法:Linq is assuming that the properties of this object are values to load from the database, and clearly it can't set the
YQM
property because it has no setter. Try making YQM a method instead: