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

标签 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           

林克

    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);

最佳答案

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

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
        );

关于c# - 如何在 Linq 左连接中使用常量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32587517/

相关文章:

c# - 异步调用时 ILNumerics 内存损坏

c# - Azure Functions 中 local.settings.json 的值

c# - 限制并发线程等于处理器数量?

c# - 如何在 lambda 表达式中返回不同的数据类型?

c# - 防止linq组中的重复和并加入

c# - 如何让 Web 应用程序从不同的服务器提供静态文件

c# - 应用面向方面的编程

c# - LINQ - 获取总查询结果时间

c# - Linq & C# - 将一个类中的不同数据插入到另一个类中

sql - 我怎么知道EF将Linq查询转换成什么?