gpt4 book ai didi

c# - 单个表达式中多个聚合值的 Linq-to-SQL 语句

转载 作者:太空狗 更新时间:2023-10-29 20:22:27 30 4
gpt4 key购买 nike

如何为以下 TSQL 编写 Linq-to-SQL 语句?

select 
count(*),
sum(Amount),
avg(Amount),
min(Amount),
max(Amount)
from
TableName

最佳答案

HACK ALERT,但它有效。尝试按照所有记录都共享的条件对您的记录进行分组:

var result = from g in db.Employees
group g by g.Id.GetType() == typeof(int) into gg
select new
{
Count = gg.Count(),
Sum = gg.Sum(x => x.Salary)
};

这会生成 SQL:

SELECT COUNT(*) AS [Count], SUM([t1].[Salary]) AS [Sum]
FROM (
SELECT 1 AS [value], [t0].[Salary]
FROM [dbo].[Employee] AS [t0]
) AS [t1]
GROUP BY [t1].[value]

这里涉及一个子查询,但是嘿!这只是一次数据库旅行

关于c# - 单个表达式中多个聚合值的 Linq-to-SQL 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2789609/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com