[英]How can I write this query in linq to sql?
I'm new in linq and want to write this query: 我是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;
How can I write that query? 如何编写该查询?
From what I assume you are trying to sum up the price_moavaq
field per year. 根据我的假设,您正在尝试总结每年的price_moavaq
字段。 Furthermore, by the use of the substring
I guess your date
field isn't of DateTime
type but just a string
. 此外,通过使用substring
我猜您的date
字段不是DateTime
类型,而只是string
。
So you need to use a groupby
: 因此,您需要使用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);
In the case that your date field is of DateTime
type then just use .Year
: 如果您的日期字段为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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.