LEFT OUTER JOIN in LINQ

后端 未结 22 3187
臣服心动
臣服心动 2020-11-21 04:49

How to perform left outer join in C# LINQ to objects without using join-on-equals-into clauses? Is there any way to do that with where clause? Corr

22条回答
  •  生来不讨喜
    2020-11-21 05:19

    Easy way is to use Let keyword. This works for me.

    from AItem in Db.A
    Let BItem = Db.B.Where(x => x.id == AItem.id ).FirstOrDefault() 
    Where SomeCondition
    Select new YourViewModel
    {
        X1 = AItem.a,
        X2 = AItem.b,
        X3 = BItem.c
    }
    

    This is a simulation of Left Join. If each item in B table not match to A item , BItem return null

提交回复
热议问题