【问题标题】:How can I write this query in linq to sql?如何在 linq to sql 中编写此查询?
【发布时间】:2016-09-09 03:04:57
【问题描述】:

我是 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_year 是什么?您能提供一些示例数据和预期结果吗?

标签: c# linq linq-to-sql


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多