.net Linq:Select 和 Where 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1212746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:05:42  来源:igfitidea点击:

Linq: What is the difference between Select and Where

.netlinqselectwhere

提问by SBurris

The Selectand Wheremethods are available in Linq. What should every developer know about these two methods? For example: when to use one over the other, any advantages of using one over the other, etc.

SelectWhereLinq中的方法可用。关于这两种方法,每个开发人员应该知道什么?例如:何时使用一种而不是另一种,使用一种优于另一种的任何优势等。

回答by Drew Noakes

Where

在哪里

finds items that match and only returns those that do (filtering).

查找匹配的项目并只返回匹配的项目(过滤)。

-> IEnumerable<A>in, IEnumerable<A>out

- >IEnumerable<A>中,IEnumerable<A>

Select

选择

returns something for allitems in the source (projection / transformation). That something might be the items themselves, but are more usually a projection of some sort.

为源中的所有项目返回一些东西(投影/转换)。某些东西可能是物品本身,但更常见的是某种投影。

-> IEnumerable<A>in, IEnumerable<B>out

- >IEnumerable<A>中,IEnumerable<B>

回答by Bruno Reis

Selectand Whereare two completely different operators acting on IEnumerables.

SelectWhere是作用于IEnumerable的两个完全不同的运算符。

The first one is what we call a Projection Operator, while the last one is a Restriction Operator.

第一个是我们所说的投影算子,而最后一个是限制算子

One interesting way to have insight on the behavior of such operators is to take a look at their "functional type".

深入了解此类运算符的行为的一种有趣方法是查看它们的“功能类型”。

  • Select : (IEnumerable<T1>, Func<T1,T2>) → IEnumerable<T2>; it takes as input both an IEnumerable containing elements of type T1 and a function transforming elements of type T1 into elements of type T2. The output is an IEnumerable containing elements of type T2.

    From this, one can easily guess that this operator will produce its output by applying the input function on each element of the input IEnumerable, and wrapping the results inside a new IEnumerable.

    Using some math-like notation, it takes as input (a, b, c, ...) : IEnumerable<T1>and f : T1 → T2and produces (f(a), f(b), f(c), ...) : IEnumerable<T2>

  • Where : (IEnumerable<T1>, Func<T1, bool>) → IEnumerable<T1> ; this one takes an IEnumerable containing elements of type T1 and a predicate on T1 (that is, a function that produces a boolean result for an input of type T1). You see that the output is also an IEnumerable containing elements of type T1.

    This time one would guess that an element of the input IEnumerable will be present on the output IEnumerable depending on the result of the application of the predicate to the element. Adding to this the semantics of the operator name, you can be sure that it will produce the output IEnumerable by taking from the input one only those elements that evaluates to true on the application of the predicate.

  • 选择: (IEnumerable<T1>, Func<T1,T2>) → IEnumerable<T2>; 它将包含 T1 类型元素的 IEnumerable 和将 T1 类型元素转换为 T2 类型元素的函数作为输入。输出是一个包含 T2 类型元素的 IEnumerable。

    由此,人们可以很容易地猜测该运算符将通过在输入 IEnumerable 的每个元素上应用输入函数,并将结果包装在一个新的 IEnumerable 中来产生其输出。

    使用一些类似数学的符号,它作为输入(a, b, c, ...) : IEnumerable<T1>f : T1 → T2并产生(f(a), f(b), f(c) , ...) : IEnumerable<T2>

  • 其中: (IEnumerable<T1>, Func<T1, bool>) → IEnumerable<T1>; 这需要一个包含 T1 类型元素的 IEnumerable 和 T1 上的谓词(即,为 T1 类型的输入生成布尔结果的函数)。您会看到输出也是一个包含 T1 类型元素的 IEnumerable。

    这一次,人们会猜测输入 IEnumerable 的元素将出现在输出 IEnumerable 上,这取决于将谓词应用于元素的结果。再加上运算符名称的语义,您可以确保它会通过从输入中仅获取那些在谓词应用中求值为真的元素来生成输出 IEnumerable。

People with functional programmingbackground usually think like this. It allows you to deduce (or at least guess...) what an operator does only by looking at it's type!

函数式编程背景的人通常是这样想的。它允许您仅通过查看运算符的类型来推断(或至少猜测......)运算符的作用!

As an exercise, try to look at other operators introduced by LINQ on IEnumerables and deduce their behavior, before looking at the documentation!

作为练习,在查看文档之前,尝试查看 LINQ 在 IEnumerables 上引入的其他运算符并推断它们的行为!

回答by bruno conde

They are distinct:

它们是不同的:

Selectis all about transformation.

Select都是关于转型

Whereis all about filtering.

Where都是关于过滤

回答by Steve

Select maps an enumerable to a new structure. If you perform a select on an IEnumerable, you will get an array with the same number of elements, but a different type depending on the mapping you specified. Where filters the IEnumerable so that it gives you a subset of the original IEnumerable.

Select 将可枚举映射到新结构。如果您在 IEnumerable 上执行选择,您将获得一个具有相同数量元素的数组,但根据您指定的映射而具有不同的类型。在哪里过滤 IEnumerable 以便它为您提供原始 IEnumerable 的子集。

回答by Downhillski

Where~= Filter

Where~= 过滤器

Select~= Map

Select~=地图

Both returns IEnumerable<T>

两者都返回 IEnumerable<T>

回答by Sanu Uthaiah Bollera

If you know how they have implemented Where and select extension methods you can predict what it is doing... I tried to implement where and select extension methods... You can have a look at it...

如果你知道他们是如何实现 Where 和 select 扩展方法的,你可以预测它在做什么......我试图实现 where 和 select 扩展方法......你可以看看它......

Where Implementation ::

哪里实施::

public static IEnumerable<Tsource> Where<Tsource> ( this IEnumerable<Tsource> a , Func<Tsource , bool> Method )
{

    foreach ( var data in a )
    {
        //If the lambda Expression(delegate) returns "true" Then return the Data. (use 'yield' for deferred return)
        if ( Method.Invoke ( data ) )
        {
            yield return data;
        }
    }
}

Select implementation ::

选择实施::

public static IEnumerable<TResult> Select<TSource , TResult> ( this IEnumerable<TSource> a , Func<TSource , TResult> Method )
{
    foreach ( var item in a )
    {
        //Each iteration call the delegate and return the Data back.(use 'yield' for deferred return)
        yield return Method.Invoke ( item );
    }
}

My implementation works fine for any collection... But it differs from Microsoft implemented Extension methods, Because they use expression trees to implement the same.

我的实现适用于任何集合......但它与 Microsoft 实现的扩展方法不同,因为它们使用表达式树来实现相同的。

回答by Supriya Bhattacherjee

In case of Select it you can map to an IEnumerable of a new structure.

在 Select it 的情况下,您可以映射到新结构的 IEnumerable。

  A.Select(x=>new X{UID=x.uid, UNAME=x.uname}) 
  //input as [IEnumerable<A>] -------->  return output as [IEnumerable<X> ]

Where() works as an filter to the IEnumerable, it will return the result on the basis of the where clause.

Where() 用作 IEnumerable 的过滤器,它将根据 where 子句返回结果。

A.Where(x=>x.uid!=0) //input as [IEnumerable<A>] -------->  return output as [IEnumerable<A> ]