我是linq的新手,想写这个查询:
var query = from p in behzad.rptost
where p.date.substring(0, 4) == "1395"
-->in this calc sum=p.price_moavaq+price_year
select p;
我该如何撰写该查询?
答案 0 :(得分:0)
根据我的假设,您每年都在尝试总结price_moavaq
字段。
此外,通过使用substring
,我猜您的date
字段不是DateTime
类型,而只是string
。
所以你需要使用groupby
:
var query = from p in behzad.rptost
group p by p.date.substring(0, 4) into grouping
select new { Year = p.Key, Sum = p.Sum(x => x.price_moavaq);
如果您的日期字段属于DateTime
类型,则只需使用.Year
:
var query = from p in behzad.rptost
group p by p.date.Year into grouping
select new { Year = p.Key, Sum = p.Sum(x => x.price_moavaq);