Linq左连接三张表备忘


//linq 语句:

from events in ASWfEvents 

join source in ASEventSources on events.EventSourceId  equals source.Id into es
from d in es.DefaultIfEmpty()
join property in ASWfEventProperties on events.Id equals property.EventId into ps
from p in ps.DefaultIfEmpty()
//where (p.Name == "OrderId" || p.Name == "_OrderId")
select new
{
events.WorkflowInstanceId,
events.Id,
events.TraceLevel,
events.EventType,
events.TimeCreated,
events.ActivityName, 
d.Name, 
d.VirtualPath,
d.Site,
d.Computer, 
p.Value

}


//对应SQL语句:

SELECT [t0].[WorkflowInstanceId], [t0].[Id], [t0].[TraceLevel], [t0].[EventType], [t0].[TimeCreated], [t0].[ActivityName], [t1].[Name] AS [Name], [t1].[VirtualPath] AS [VirtualPath], [t1].[Site] AS [Site], [t1].[Computer] AS [Computer], [t2].[Value] AS [Value]
FROM [ASWfEvents] AS [t0]
LEFT OUTER JOIN [ASEventSources] AS [t1] ON [t0].[EventSourceId] = [t1].[Id]
LEFT OUTER JOIN [ASWfEventProperties] AS [t2] ON [t0].[Id] = [t2].[EventId]


//对应lambda表达式:

ASWfEvents
   .GroupJoin (
      ASEventSources, 
      events => events.EventSourceId, 
      source => source.Id, 
      (events, es) => 
         new  
         {
            events = events, 
            es = es
         }
   )
   .SelectMany (
      temp0 => temp0.es.DefaultIfEmpty (), 
      (temp0, d) => 
         new  
         {
            temp0 = temp0, 
            d = d
         }
   )
   .GroupJoin (
      ASWfEventProperties, 
      temp1 => temp1.temp0.events.Id, 
      property => property.EventId, 
      (temp1, ps) => 
         new  
         {
            temp1 = temp1, 
            ps = ps
         }
   )
   .SelectMany (
      temp2 => temp2.ps.DefaultIfEmpty (), 
      (temp2, p) => 
         new  
         {
            WorkflowInstanceId = temp2.temp1.temp0.events.WorkflowInstanceId, 
            Id = temp2.temp1.temp0.events.Id, 
            TraceLevel = temp2.temp1.temp0.events.TraceLevel, 
            EventType = temp2.temp1.temp0.events.EventType, 
            TimeCreated = temp2.temp1.temp0.events.TimeCreated, 
            ActivityName = temp2.temp1.temp0.events.ActivityName, 
            Name = temp2.temp1.d.Name, 
            VirtualPath = temp2.temp1.d.VirtualPath, 
            Site = temp2.temp1.d.Site, 
            Computer = temp2.temp1.d.Computer, 
            Value = p.Value
         }
   )


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值