如何在Linq左连接中使用常量值?

时间:2015-09-15 13:34:54

标签: c# linq

我需要在左连接中使用常量值,如Linq表达式中的sql所示。如何在linq表达中实现这一点。

 SELECT *
    FROM [dbo].[s1AlertLog] AS [Extent1]            
  LEFT OUTER JOIN [dbo].[smManualChapter] AS [Extent4] ON [Extent1].[mwEventDetailKey] = [Extent4].[ID] and [Extent1].mwEventTypeKey = 300  Where Extent1.mwcUsersKey = 8000           

LINQ的

    var list = (from alert in ctx.Set<S1AlertLog>()
                      join smManualChapter in ctx.Set<SmManualChapter>() on new { alert.MwEventDetailKey, alert.MwEventTypeKey } equals new { smManualChapter.ID, 300}
                          into temp1 
                      from tempChapter in temp1.DefaultIfEmpty()
                      select  alert
            );

  var result = list.Where(x=>x.mwcUsersKey == 8000);

1 个答案:

答案 0 :(得分:2)

您需要匿名类型中的相同字段名称:

var list = (from alert in ctx.Set<S1AlertLog>()
              join smManualChapter in ctx.Set<SmManualChapter>() 
                  on new { d = alert.MwEventDetailKey, t = alert.MwEventTypeKey } 
              equals new { d = smManualChapter.ID    , t = 300 }
                  into temp1 
              from tempChapter in temp1.DefaultIfEmpty()
              select  alert
        );