select 基于计数

select basing on count

我有一张table账单。

这个table有一个字段Id_Client

我想恢复一个列表,其中包含超过多个账单的客户。

在SQL,我做了这样的事情

select Id_Client, Count(*)
from MyDB.dbo.Bill
group by Id_Client
having Count(*) > 100
order by c desc

其中 100 是此限制的示例。

我想在 Entity Framework - LINQ 中执行此操作。

试试这个:

var query= db.Bills.GroupBy(b => b.Id_Client)
                   .Select(g => new {Id_Client = g.Key, Count = g.Count()})
                   .Where(r => r.Count > 100)
                   .OrderByDescending(r=>r.Count);

此 Linq 查询将在 sql 中转换为此查询:

{SELECT 
[GroupBy1].[K1] AS [Id_Client], 
[GroupBy1].[A1] AS [C1]
FROM ( SELECT 
    [Extent1].[Id_Client] AS [K1], 
    COUNT(1) AS [A1]
    FROM [dbo].[Bill] AS [Extent1]
    GROUP BY [Extent1].[Id_Client]
)  AS [GroupBy1]
WHERE [GroupBy1].[A1] > 100
ORDER BY [GroupBy1].[A1] DESC}