如何在linq中将此查询写入sql?

时间:2016-09-06 04:58:39

标签: c# linq linq-to-sql

我是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;

我该如何撰写该查询?

1 个答案:

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