LINQ - 按问题分组

时间:2017-01-06 18:27:02

标签: c# sql-server linq

我试图转换这样的SQL查询

select qb.Fabricator_id, cj.Client_id,
    (select Name from CustomerBase where Id = qb.Fabricator_id),
    (select ContactEmail from CustomerBase where Id = qb.Fabricator_id)
from ClientJob cj, Quote q, QuoteBid qb
where cj.Client_id in (39343, 39157)
and q.ClientJob_id = cj.Id
and qb.Quote_id = q.Id
group by qb.Fabricator_id, cj.Client_id
order by cj.Client_id, qb.Fabricator_id

进入LINQ,到目前为止我有这样的查询

var customerId = new[] {39343, 39157};

var query = from cj in _dataContext.ClientJob join q in _dataContext.Quote on cj.Id equals q.ClientJob_id
             join qb in _dataContext.QuoteBid on q.Id equals qb.Quote_id
            where customerId.Contains(cj.Client_id.Value)
            && qb.Fabricator_id.HasValue
            group new { CustomerId = cj.Client_id, FabricatorId = qb.Fabricator_id } by new { qb.Fabricator_id, cj.Client_id} into uniqueFabricators
            from uf in uniqueFabricators
            orderby uf.CustomerId, uf.FabricatorId
            select new
            {
                uf.CustomerId,
                uf.FabricatorId,
                FabricatorName = _dataContext.CustomerBase.Single(_ => _.Id == uf.FabricatorId).Name,
                FabricatorEmail = _dataContext.CustomerBase.Single(_ => _.Id == uf.FabricatorId).ContactEmail
            };

看起来非常类似于SQL,但结果完全不同。

如果我在LINQPad中执行它,结果SQL查询看起来有点类似,但不完全。

-- Region Parameters
DECLARE @p0 Int = 39343
DECLARE @p1 Int = 39157
-- EndRegion
SELECT [t4].[Client_id] AS [CustomerId], [t6].[Fabricator_id] AS [FabricatorId], (
    SELECT [t7].[Name]
    FROM [CustomerBase] AS [t7]
    WHERE ([t7].[Id]) = [t6].[Fabricator_id]
    ) AS [FabricatorName], (
    SELECT [t8].[ContactEmail]
    FROM [CustomerBase] AS [t8]
    WHERE ([t8].[Id]) = [t6].[Fabricator_id]
    ) AS [FabricatorEmail]
FROM (
    SELECT [t2].[Fabricator_id], [t0].[Client_id]
    FROM [ClientJob] AS [t0]
    INNER JOIN [Quote] AS [t1] ON ([t0].[Id]) = [t1].[ClientJob_id]
    INNER JOIN [QuoteBid] AS [t2] ON ([t1].[Id]) = [t2].[Quote_id]
    WHERE (([t0].[Client_id]) IN (@p0, @p1)) AND ([t2].[Fabricator_id] IS NOT NULL)
    GROUP BY [t2].[Fabricator_id], [t0].[Client_id]
    ) AS [t3]
CROSS JOIN ([ClientJob] AS [t4]
    INNER JOIN [Quote] AS [t5] ON ([t4].[Id]) = [t5].[ClientJob_id]
    INNER JOIN [QuoteBid] AS [t6] ON ([t5].[Id]) = [t6].[Quote_id])
WHERE ((([t3].[Fabricator_id] IS NULL) AND ([t6].[Fabricator_id] IS NULL)) OR (([t3].[Fabricator_id] IS NOT NULL) AND ([t6].[Fabricator_id] IS NOT NULL) AND ((([t3].[Fabricator_id] IS NULL) AND ([t6].[Fabricator_id] IS NULL)) OR (([t3].[Fabricator_id] IS NOT NULL) AND ([t6].[Fabricator_id] IS NOT NULL) AND ([t3].[Fabricator_id] = [t6].[Fabricator_id]))))) AND ((([t3].[Client_id] IS NULL) AND ([t4].[Client_id] IS NULL)) OR (([t3].[Client_id] IS NOT NULL) AND ([t4].[Client_id] IS NOT NULL) AND ((([t3].[Client_id] IS NULL) AND ([t4].[Client_id] IS NULL)) OR (([t3].[Client_id] IS NOT NULL) AND ([t4].[Client_id] IS NOT NULL) AND ([t3].[Client_id] = [t4].[Client_id]))))) AND (([t4].[Client_id]) IN (@p0, @p1)) AND ([t6].[Fabricator_id] IS NOT NULL)
ORDER BY [t4].[Client_id], [t6].[Fabricator_id]

我的LINQ查询出了什么问题,无法看到问题。

THX

2 个答案:

答案 0 :(得分:0)

权利查询将是

    var query = from cj in _dataContext.ClientJob
     join q in _dataContext.Quote on cj.Id equals q.ClientJob_id
     join qb in _dataContext.QuoteBid on q.Id equals qb.Quote_id
     where customerId.Contains(cj.Client_id.Value)
     && qb.Fabricator_id.HasValue
     group new { cj.Client_id, qb.Fabricator_id } by new { qb.Fabricator_id, cj.Client_id } into uniqueFabricators
     orderby uniqueFabricators.Key.Client_id, uniqueFabricators.Key.Fabricator_id
        select new
        {
            uniqueFabricators.Key.Client_id,
            uniqueFabricators.Key.Fabricator_id,
            FabricatorName = _dataContext.CustomerBase.Single(_ => _.Id == uniqueFabricators.Key.Fabricator_id).Name,
            FabricatorEmail = _dataContext.CustomerBase.Single(_ => _.Id == uniqueFabricators.Key.Fabricator_id).ContactEmail
        };

无需使用其他子查询from uniqueFabricators

答案 1 :(得分:0)

没有必要使用JOIN,在原始SQL请求中它不是。

nvm install v6.9.1