gpt4 book ai didi

linq-to-sql - Linq 到 SQL : how to aggregate without a group by?

转载 作者:行者123 更新时间:2023-12-03 09:05:01 26 4
gpt4 key购买 nike

我正在搜索与此查询等效的 Linq-to-SQL:

SELECT
[cnt]=COUNT(*),
[colB]=SUM(colB),
[colC]=SUM(colC),
[colD]=SUM(colD)
FROM myTable

这是 没有组的聚合 .除了发出四个单独的查询(一个计数和三个总和)之外,我似乎找不到任何方法来做到这一点。有任何想法吗?

最佳答案

这就是我发现的似乎你仍然需要做一个组...可以只使用常量:

var orderTotals =
from ord in dc.Orders
group ord by 1 into og
select new
{
prop1 = og.Sum(item=> item.Col1),
prop2 = og.Sum(item => item.Col2),
prop3 = og.Count(item => item.Col3)
};

这会产生以下 SQL,这不是最佳的,但有效:
SELECT SUM([Col1]) as [prop1], SUM([Col2]) as [prop2], COUNT(*) AS [prop3]
FROM (
SELECT 1 AS [value], [t0].[Col1], [t0].[Col2], [t0].[Col3]
FROM [table] AS [t0]
) AS [t1]
GROUP BY [t1].[value]

关于linq-to-sql - Linq 到 SQL : how to aggregate without a group by?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1597181/

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