【问题标题】:How to sum the multiplied columns from subquery in linq如何对linq中子查询的相乘列求和
【发布时间】:2023-03-17 13:31:01
【问题描述】:

我正在尝试将表中的列相乘并使用子查询我尝试对相乘的列求和

select B.ID , sum(B) from (
select distinct A.Id, 
A.Savings * A.Spent as B,
from A group by A.id ) B
group by B.ID

如何将此 sql 查询转换为 linq。

【问题讨论】:

    标签: sql sql-server entity-framework linq linq-to-sql


    【解决方案1】:

    此时您的查询是错误的:

    A.Savings * A.Spent as B,
    from A group by A.id
    

    from 子句前的逗号会导致查询出错。试试下面的:

    SELECT
    B.ID,
    SUM(Total) AS TOTAL
    FROM 
    (SELECT
     A.Savings * A.Spent as Total
     From A
     Group by A.ID)
    Group by B.ID
    

    您的查询是正确的,您可能只需要在以后检查。

    【讨论】:

    • 问题是如何将此 sql 查询转换为 linq。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多