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

前端之家收集整理的这篇文章主要介绍了c# – 单个表达式中多个聚合值的Linq-to-SQL语句前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何为以下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]

有一个子查询涉及,但嘿!这只是一次db旅行

原文链接:https://www.f2er.com/csharp/99114.html

猜你在找的C#相关文章