Is Linq Where's More Expensive than Linq Select?

I am studying work on an operation. I repeat a subset of the items from the collection. I am filtering this collection using a Linq query. It basically looks like this:

var filteredItems = items.Where(x => x.PropertyToFilterOn == filterValue);
foreach (var filteredItem in filteredItems)
{
    // do something to the filtered item
}

If I use Selectinstead Where, I do the same. What is better to use and what is the difference?

+3
source share
3 answers

Where and choose, of course, do not achieve the same.

Wherefilters an enumerated value based on a predicate. The result of calling Where on IEnumerable<T>is equal IEnumerable<T>.

Select - - , , , .

O (n) - .

.

http://download.microsoft.com/download/5/8/6/5868081c-68aa-40de-9a45-a3803d8134b8/standard_query_operators.doc

+9

Where, Select. .

+5

, :

// Where
foreach(var x in items)
{
    if (x.PropertyToFilterOn == filterValue)
        yield return x;
}

// Select
foreach (var x in items)
{
    yield return selector(x);
}

, , -. , , .

+2

Source: https://habr.com/ru/post/1728583/


All Articles