简体   繁体   中英

How can I write this query in linq to sql?

I'm new in linq and want to write this query:

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. Furthermore, by the use of the substring I guess your date field isn't of DateTime type but just a string .

So you need to use a 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 :

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM