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

How can I write this query in 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;

我该如何编写该查询?

根据我的假设,您正在尝试总结每年的 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);