【发布时间】:2020-04-07 05:45:30
【问题描述】:
有两个表,一个是customers 有customerID,GroupID 字段,另一个是CustomerGroup 有GroupID,GroupName 字段,我想得到customerID 的数量在每个组中,这是 LINQ 语句:
var groups = from customerGroups in db.CustomerGroup
join customers in db.Customers on customerGroups.GroupID equals customers.GroupID into gc
where customerGroups.MerchantID == merchantID
from subCustomerGroups in gc.DefaultIfEmpty()
group customerGroups by customerGroups.GroupName into grpCustomerGroups
select new { GroupName = grpCustomerGroups.Key, Quantity = customers.Count()};
问题是Quantity = customers.Count()无效,如何更正该语句?
预期的 sql 语句是
exec sp_executesql N'SELECT
1 AS [C1],
[GroupBy1].[K1] AS [GroupName],
[GroupBy1].[A1] AS [C2]
FROM ( SELECT
[Extent1].[GroupName] AS [K1],
COUNT(CustomerID) AS [A1]
FROM [dbo].[CustomerGroup] AS [Extent1]
LEFT OUTER JOIN [dbo].[Customer] AS [Extent2] ON [Extent1].[GroupID] = [Extent2].[GroupID]
WHERE [Extent1].[MerchantID] = @p__linq__0
GROUP BY [Extent1].[GroupName]
) AS [GroupBy1]',N'@p__linq__0 bigint',@p__linq__0=9
【问题讨论】:
标签: c# linq linq-to-sql