LINQ Join with Multiple Conditions in On Clause

前端 未结 4 789
小鲜肉
小鲜肉 2020-11-28 05:25

I\'m trying to implement a query in LINQ that uses a left outer join with multiple conditions in the ON clause.

I\'ll use the example of the following two tables

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 05:41

    You just need to name the anonymous property the same on both sides

    on new { t1.ProjectID, SecondProperty = true } equals 
       new { t2.ProjectID, SecondProperty = t2.Completed } into j1
    

    Based on the comments of @svick, here is another implementation that might make more sense:

    from t1 in Projects
    from t2 in Tasks.Where(x => t1.ProjectID == x.ProjectID && x.Completed == true)
                    .DefaultIfEmpty()
    select new { t1.ProjectName, t2.TaskName }
    

提交回复
热议问题