且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

实体框架使用join方法和lambdas加入

更新时间:2022-11-05 12:19:43

我喜欢LINQ的lambda语法,但是 Join 是一个例子,我喜欢查询语法 - 纯粹为了可读性。

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.