Entityframework Join using join method and lambdas

前端 未结 3 1578
无人及你
无人及你 2020-12-04 12:50

It seems there are different ways to do joins using linq. One is more straightforward and involves just joining tables like this:

var found = from c in s.cat         


        
3条回答
  •  时光说笑
    2020-12-04 13:55

    Generally i prefer the lambda syntax with LINQ, but Join is one example where i prefer the query syntax - purely for readability.

    Nonetheless, here is the equivalent of your above query (i think, untested):

    var query = db.Categories         // source
       .Join(db.CategoryMaps,         // target
          c => c.CategoryId,          // FK
          cm => cm.ChildCategoryId,   // PK
          (c, cm) => new { Category = c, CategoryMaps = cm }) // project result
       .Select(x => x.Category);  // select result
    

    You might have to fiddle with the projection depending on what you want to return, but that's the jist of it.

提交回复
热议问题