实体框架使用join方法和lambdas加入 [英] Entityframework Join using join method and lambdas

查看:84
本文介绍了实体框架使用join方法和lambdas加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有不同的方式使用linq进行联接。一个更直接,只需要加入这样的表:

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.categories
            join cm in s.categorymaps on c.CategoryId equals cm.ChildCategoryId
            select c;

还有另一种使用lambdas的方式,我有一段时间想知道如何使用这种语法加入。任何人都可以提供详细的解释和很多例子的链接?或者只是简单地演示使用示例如何使用这种相当混乱的语法?

There is another way using lambdas and I'm having a heck of a time trying to figure out how to join using this syntax. Can anyone provide links to detailed explanations and lots of examples? Or just simply demonstrate using examples how to use this rather confusing syntax?

var x = _session.All<category>().Join<categorymap,category, ....


推荐答案

我喜欢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.

这篇关于实体框架使用join方法和lambdas加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆