LINQ的“何处"如何处理?方法有效吗? [英] How does LINQ's "Where" method works?

查看:52
本文介绍了LINQ的“何处"如何处理?方法有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

究竟如何定义LINQ的"where"方法?我猜实现是这样的:

Exactly how LINQ's "where" method is defined? I'm guessing implementation is something like this:

public static IEnumerable<T> Where ( this partialParent, Func<bla,bla> myDelegate ) 

现在,如果我这样调用Where方法:

Now if I invoke Where method like this:

from c in context.Con
where ( c.Col1 == c.Col2 )
select c

我猜想"c.Col1 == c.Col2"被传递了,并且某些foreach循环进行了检查.但是当我在这样的地方调用时发生了什么:

I'm guessing "c.Col1 == c.Col2" is passed down and some foreach loop does the checking. But what is going on when I invoke where like this:

where ( c.Col1 == c.Col2 || c.Col3 == c.Col4 )

是否将两个检查"作为一个整体传递下来?也许我缺少一些非常简单的东西.

Does the two "checks" are passed down as a whole expression? Maybe I'm missing something very simple.

推荐答案

您为where提到的查询语法基本上会创建一个方法和委托,并使用它来调用方法语法版本.无论您用什么调用where,它都会转换为一个方法,然后通过委托对源序列的每个元素进行调用.

The query syntax you mention for where basically creates a method and delegate, and calls the method syntax version with it. Whatever you call where with is turned into a single method and then called via a delegate on each element of the source sequence.

因此,将两个检查作为一个整体表达式传递的含义是什么?那就是正在发生的事情...

So possibly what you mean by the two checks being passed as a whole expression; that's what is happening...

where ( c.Col1 == c.Col2 || c.Col3 == c.Col4 )

它变成了这样的单个方法调用:

it turned into a single method call like this:

bool MethodExample(var c){
  return ( c.Col1 == c.Col2 || c.Col3 == c.Col4 );
}

,然后在每个元素上调用. (显然上面是伪代码)

and is then called on each element. (obviously the above is pseudo-code)

这篇关于LINQ的“何处"如何处理?方法有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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