多个JOINS及其之间的条件:LINQ查询

时间:2019-02-22 15:04:16

标签: c# sql .net linq lambda

我正在尝试将SQL查询转换为c#等效的LINQ / Lambda,如下所示

SELECT  mv.MeasureID, mv.ProviderID, MAX(mv.ReportingDate) AS mostRecentReportingDate
FROM    (((TransactionDetail AS td
           INNER JOIN #provider AS p ON td.TrustCode = p.Code)
           INNER JOIN #measure AS m ON td.MetricCode = m.InternalID)
           INNER JOIN #measureValue AS mv ON m.ID = mv.MeasureID AND p.ID = mv.ProviderID)
WHERE   td.BatchID = @batchID AND
        td.RowAction = 'A' AND
        (m.Type = 7 OR m.Type = 8) AND
        td.Value <> mv.Value
GROUP BY mv.MeasureID, mv.ProviderID

我被困在这一行

INNER JOIN #measureValue AS mv ON m.ID = mv.MeasureID AND p.ID = mv.ProviderID)

以下是我到目前为止尝试过的

 var total = (from TD in ingestionHubContext.TransactionDetail
                    join P in provider on TD.TrustCode equals P.Code
                    join M in measure on TD.MetricCode equals M.InternalId
                    join MV in measureValue on M.Id equals MV.MeasureId //and logic to be fixed
                    where TD.BatchId == batchId && TD.RowAction == "A"
                    && (M.Type == 7 || M.Type == 8) && TD.Value != MV.Value
                    group TD by new { MV.MeasureId, MV.ProviderId } into Total
                    select Total);

您能否建议我如何更好地编写此书?并在我的代码的第4行中使用AND条件处理JOIN。

我在下面尝试了以下方法,因为它们需要匹配(在  P.Id)

join MV in measureValue 
on new { M.id, P.Id } equals new { MV.MeasureId, MV.ProviderId }

我在做MAX(mv.ReportingDate)

时也需要帮助

非常感谢您的帮助。预先感谢。

3 个答案:

答案 0 :(得分:2)

当ON子句中有多个字段时,需要使用对象进行比较。 您的LINQ查询应编写如下:

    var total = (from TD in ingestionHubContext.TransactionDetail
                        join P in provider on TD.TrustCode equals P.Code
                        join M in measure on TD.MetricCode equals M.InternalId
// Here is how
                        join MV in measureValue on new {M.Id, P.Id} equals new {MV.MeasureId, MV.ProviderId}
// the rest is your original where statement
                        where TD.BatchId == batchId && TD.RowAction == "A"
                        && (M.Type == 7 || M.Type == 8) && TD.Value != MV.Value
                        group TD by new { MV.MeasureId, MV.ProviderId } into Total
                        select Total);

答案 1 :(得分:1)

我通过编写如下查询来解决

join MV in measureValue on new { mID = M.Id, pID = P.Id } equals new {mID = MV.MeasureId, pID = MV.ProviderId }

这允许使用多个表列的条件。

如果有人可以帮助更有效地重写它,我仍然很感激。

谢谢。

答案 2 :(得分:1)

对不起,我没有看到您的第二个问题。 最终的LINQ查询将实现您要寻找的内容,包括Max,将是以下语句:

var query = from td in transactionDetails
                        join p in providers on td.TrustCode equals p.Code
                        join m in measures on td.MetricCode equals m.InternalId
                        join mv in measureValues on new { mId = m.Id, pId = p.Id } equals new { mId = mv.MeasureId, pId = mv.ProviderId }
                        where td.BatchId == batchId && td.RowAction == "A"
                        && (m.Type == 7 || m.Type == 8) && td.Value != mv.Value
                        select new { td, p, m, mv } into queryList
                        group queryList by new { queryList.mv.MeasureId, queryList.mv.ProviderId } into groupedList
                        select new {
                            groupedList.Key.MeasureId,
                            groupedList.Key.ProviderId,
                            mostRecentReportingDate = groupedList.Max(g => g.mv.ReportingDate) };

我将简化名称用于提供者,度量等的集合,您可以将真正的提供者放在那里。

此LINQ查询将返回原始SQL查询的结果,并将通过SQL Server对其进行尽可能的优化,对于在LINQ to DB上进行的优化您无能为力...

祝你好运!