【问题标题】:Linq inner join on complex query复杂查询的Linq内连接
【发布时间】:2016-08-09 08:30:06
【问题描述】:
var workbasketitems = new[] { //workbasketitems
    new { TaskId = 10, WorkGroupId = 100, ActionUserId =1000, Work = "item 0" },
    new { TaskId = 11, WorkGroupId = 101, ActionUserId =1001, Work = "item 1" },
    new { TaskId = 12, WorkGroupId = 102, ActionUserId =1002, Work = "item 2" }
};
var workflowtasks = new[] { //workflowtasks
    new { TaskId = 10, TaskDesc  = "TaskDesc 0" },
    new { TaskId = 11, TaskDesc  = "TaskDesc 1" },
    new { TaskId = 12, TaskDesc  = "TaskDesc 2" }
};
var workgroup = new[] { //workgroup
    new { WorkGroupId = 100, WGDesc  = "WGDesc 0" },
    new { WorkGroupId = 101, WGDesc  = "WGDesc 1" },
    new { WorkGroupId = 102, WGDesc  = "WGDesc 2" }
};
var applicationuser = new[] { //applicationuser
    new { AUId = 1000, AUDesc  = "AUId 0" },
    new { AUId = 1001, AUDesc  = "AUId 1" }
};
    var results = from wb in workbasketitems 
        join wft in workflowtasks on wb.TaskId equals wft.TaskId
        join wg in workgroup on wb.WorkGroupId equals wg.WorkGroupId        
        select new { wft.TaskDesc, wg.WGDesc, wb.ActionUserId, wb.Work};
    results.Dump();

    var resultsInner = from wb in results 
        join au in applicationuser on wb.ActionUserId equals au.AUId into wbl
        from auList in wbl.DefaultIfEmpty()
        select new { wb.TaskDesc, wb.WGDesc, Desc = (auList == null? "BlAnk": auList.AUDesc), wb.Work};     
    resultsInner.Dump();

有没有一种方法可以组合下面的 Linq 查询,因为我想让事情保持可维护性,这很有帮助。以上在 Linqpad 中工作,resultsInner 是新创建的结果表的内连接。

【问题讨论】:

    标签: c# mysql linq linqpad


    【解决方案1】:

    你快到了。只需将您在第二个查询中执行的额外 left join 添加到第一个查询:

    var results = from wb in workbasketitems
                  join wft in workflowtasks on wb.TaskId equals wft.TaskId
                  join wg in workgroup on wb.WorkGroupId equals wg.WorkGroupId
                  join au in applicationuser on wb.ActionUserId equals au.AUId into wbl
                  from auList in wbl.DefaultIfEmpty()
                  select new { wft.TaskDesc, wg.WGDesc, Desc = (auList == null ? "BlAnk" : auList.AUDesc), wb.Work };
    

    您还可以使用DefaultIfEmpty 的另一个重载来指定在left join 结果为null 时要执行的操作:

    var results = from wb in workbasketitems
                  join wft in workflowtasks on wb.TaskId equals wft.TaskId
                  join wg in workgroup on wb.WorkGroupId equals wg.WorkGroupId
                  join au in applicationuser on wb.ActionUserId equals au.AUId into wbl
                  from auList in wbl.DefaultIfEmpty(new { AUId = wb.ActionUserId, AUDesc = "Blank" } )
                  select new { wft.TaskDesc, wg.WGDesc, Desc = auList.AUDesc, wb.Work };
    

    【讨论】:

    • 非常好,非常感谢。希望我能给你更多的分数来获得这样一个完整的答案...... :)
    【解决方案2】:

    你在找这个吗:

    var results = from wb in workbasketitems 
        join wft in workflowtasks on wb.TaskId equals wft.TaskId
        join wg in workgroup on wb.WorkGroupId equals wg.WorkGroupId
        join au in applicationuser on wb.ActionUserId equals au.AUId into wbl
        from auList in wbl.DefaultIfEmpty()
        select new { wft.TaskDesc, wg.WGDesc, Desc = (auList == null? "Blank": auList.AUDesc), wb.Work};
    

    对可维护性没有影响。但在这种情况下,您省略了额外的预测。

    【讨论】:

      猜你喜欢
      • 2018-10-14
      • 2020-03-06
      • 2012-11-29
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 2019-06-20
      • 2010-11-02
      • 1970-01-01
      相关资源
      最近更新 更多