How to work with optional int parameter in LINQ Where ()

Please note that this is not a question of what delegates are in general. Also, I saw docs without getting wiser.

In LINQ, I can use something like this.

using(Model model = new Model())
{
   return model.Things
               .Where(thing => thing.IsGood);
}

I see that the return type (left operator object) has a type Thing, and the condition (right operator object) has a type bool. Intellisense tells me that I can choose from these two, and I was confused by the second.

Func<Thing, bool>  
Func<Thing, int, bool>

I assume the lambda operator is just syntactic sugar for the actual call. It's right? If so, what is this integer and how to specify it?

+4
source share
5

:

: System.Func<TSource, Int32, Boolean>

; .

, , .

,

var everyTwo = input.Where((c, i) => i % 2 == 0);

, . , .

, , , , , , , .

+4

, , : -

. index .

, , , : -

public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, 
                                                   Func<TSource, int, bool> predicate) 
{
    if (source == null) throw Error.ArgumentNull("source");
    if (predicate == null) throw Error.ArgumentNull("predicate");
    return WhereIterator<TSource>(source, predicate);
}

static IEnumerable<TSource> WhereIterator<TSource>(IEnumerable<TSource> source, 
                                                   Func<TSource, int, bool> predicate) 
{
   int index = -1;
   foreach (TSource element in source) {
       checked { index++; }
       if (predicate(element, index)) yield return element;
   }
}

, , , foreach. , : -

.Where((thing, index) => thing.SomeIntProperty <= index * 2);
+2

, MSDN:

:

var list = new List<string> { "a", "b", "c" }
var index = 0;
foreach (var item in list)
{
    if (item == "b" && index == 1) {
        Console.WriteLine(item);
    }
    index++;
}

var list = new List<string> { "a", "b", "c" }

var items = list.Where((item, index) => item == "b" && index == 1)

foreach (var item in items) {
    Console.WriteLine(item);
}

( linq .., Where)

+2

MSDN:

. index .

? , :

return model.Things
    .Where((thing, index) => index % 2 == 0);

, , -, .net . , overlaod Where():

 public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate) {
            if (source == null) throw Error.ArgumentNull("source");
            if (predicate == null) throw Error.ArgumentNull("predicate");
            return WhereIterator<TSource>(source, predicate);
        }

, WhereIterator:

static IEnumerable<TSource> WhereIterator<TSource>(IEnumerable<TSource> source, Func<TSource, int, bool> predicate) {
            int index = -1;
            foreach (TSource element in source) {
                checked { index++; }
                if (predicate(element, index)) yield return element;
            }
        }

, Linq foreach , index .

+1

"" MSDN. , , .

.

using(Model model = new Model())
{
  return model.Things
    .Where((thing, counter) => thing.IsGood < counter);
}

As you can see, this makes little sense in this particular case, and I never needed to use it. Nevertheless, if such a need arises, he is there. You can find an example at the provided link.

0
source

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


All Articles