编写LINQ C#的方法 [英] Methods of writing LINQ C#

查看:74
本文介绍了编写LINQ C#的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下内容感到困惑.

I'm perplexed by the below.

我在项目中一直使用LINQ,格式如下:

I've been using LINQ in my projects using formats such as:

var query =     
    from obj_i in set1
    join obj_j in set2 on 
        new { 
          JoinField1 = obj_i.SomeField1,
          JoinField2 = obj_i.SomeField2,
          JoinField3 = obj_i.SomeField3,
          JoinField4 = obj_i.SomeField4
        } 
        equals 
        new { 
          JoinField1 = obj_j.SomeOtherField1,
          JoinField2 = obj_j.SomeOtherField2,
          JoinField3 = obj_j.SomeOtherField3,
          JoinField4 = obj_j.SomeOtherField4
        }

但是最近我被告知,以下也是编写LINQ查询的另一种方式".

But I was recently told that the below is also 'another way' of writing LINQ queries.

var query =     
    from obj_i in set1
    join obj_j in set2 on       
    obj_i.SomeField1 = obj_j.SomeOtherField1 and
    obj_i.SomeField2 = obj_j.SomeOtherField2 and
    obj_i.SomeField3 = obj_j.SomeOtherField3 and
    obj_i.SomeField4 = obj_j.SomeOtherField4

据我所知,使用单个=是错误的(特别是在由于需要使用equals而不适用​​于==的情况下,但是使用and也是错误的,因为正确如果在这种情况下允许您使用除equals之外的任何内容,则关键字将为&&.

As I understand, using the single = is wrong (especially in the case where by == doesn't apply since you need to use equals, but also using and is wrong, since the correct keyword would be && if you were allowed to use anything but equals in this case.

我可以理解where子句中&&==的使用,这使得上面的代码可以使用,因为它甚至不能编译,因此更加出色.

I can understand the use of && and == in the where clause, which makes it even more outstanding that the above code can be used, since it doesn't even compile.

我错过了什么吗? 如果是这样,您能指出我在哪里可以学习到这种替代的LINQ编写方法吗?

Am I missing something? If so, could you point me to where I can learn of this alternate method of writing LINQ?

推荐答案

但是最近我被告知,以下也是编写LINQ查询的另一种方式".

But I was recently told that the below is also 'another way' of writing LINQ queries.

否,您显示的第二种语法不正确.尝试一下,您会发现它无法编译.

No, the second syntax you show is incorrect. Just try it, you'll see that it doesn't compile.

Linq查询理解语法中的join子句被转换为对 Join 扩展方法.例如,此查询:

The join clause in Linq query comprehension syntax is translated to a call to the Join extension method. For instance, this query:

var query =
    from x in a
    join y in b
    on x.Foo equals y.Bar
    select Baz(x, y);

已翻译为:

var query = a.Join(b, x => x.Foo, y => y.Foo, (x, y) => Baz(x, y));

因此您可以看到equals左右部分分别对应于Join方法的不同参数:左侧部分选择第一个源的连接键,右侧部分选择第二个来源.然后,将这些键彼此匹配以执行连接.这是重要的部分:on子句未指定自由格式的连接条件,它指定了如何在每一侧提取连接键.因此,左右键选择器必须清楚地分开;您显示的第二种语法不起作用,因为无法提取任何一个源的完整密钥信息:只能通过评估每个(x, y)的联接条件来执行联接,这是一个O(n²)操作,而不是一个(大致)O(n)操作.

So you can see that the parts on the left and right of equals correspond to different parameters of the Join method: the left part selects the join key for the first source, and the right part selects the key for the second source. These keys are then matched against each other to perform the join. This is the important part: the on clause doesn't specify a free-form join condition, it specifies how to extract the join key on each side. So the left and right key selectors have to be clearly separated; the second syntax you show can't work, because there's no way to extract full key information for either source: the join could only be performed by evaluating the join condition for each (x, y), which is an O(n²) operation instead of a (roughly) O(n) operation.

但是,有时候您需要的灵活性超出了等值连接的能力;在这种情况下,您可以使用交叉联接并过滤结果:

However, sometimes you need more flexibility than what an equi-join can give; in that case you can use a cross-join and filter the results:

var query =
    from x in a
    from y in b
    where x.Foo == y.Bar
    select Baz(x, y);

或者您的情况:

var query =     
    from obj_i in set1
    from obj_j in set2
    where obj_i.SomeField1 == obj_j.SomeOtherField1
    &&    obj_i.SomeField2 == obj_j.SomeOtherField2
    &&    obj_i.SomeField3 == obj_j.SomeOtherField3
    &&    obj_i.SomeField4 == obj_j.SomeOtherField4;

这篇关于编写LINQ C#的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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