如何使用方法语法在 linq 中加入 sql? [英] How to do a join in linq to sql with method syntax?

查看:19
本文介绍了如何使用方法语法在 linq 中加入 sql?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 LINQ to SQL 示例中看到了很多关于如何在查询语法中进行连接的示例,但我想知道如何使用方法语法进行连接?例如,我将如何执行以下操作

I have seen lots of examples in LINQ to SQL examples on how to do a join in query syntax but I am wondering how to do it with method syntax? For example how might I do the following

var result = from sc in enumerableOfSomeClass
             join soc in enumerableOfSomeOtherClass
             on sc.Property1 equals soc.Property2
             select new { SomeClass = sc, SomeOtherClass = soc }

带有 .Join()?谁能说明或提供另一个简单的例子?

with a .Join()? Can anyone illustrate or provide another simple example?

推荐答案

var result = from sc in enumerableOfSomeClass
             join soc in enumerableOfSomeOtherClass
             on sc.Property1 equals soc.Property2
             select new { SomeClass = sc, SomeOtherClass = soc };

相当于:

var result = enumerableOfSomeClass
    .Join(enumerableOfSomeOtherClass,
          sc => sc.Property1,
          soc => soc.Property2,
          (sc, soc) => new
                       {
                           SomeClass = sc,
                           SomeOtherClass = soc
                       });

如您所见,在连接方面,查询语法通常比 lambda 语法更具可读性.

As you can see, when it comes to joins, query syntax is usually much more readable than lambda syntax.

这篇关于如何使用方法语法在 linq 中加入 sql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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