I am confused if the parameter fruit (which I know is an input parameter) is returned if the condition is true for predicate. As the following piece of code signifies:
List<string> fruits = new List<string> {
"apple",
"passionfruit",
"banana",
"mango",
"orange",
"blueberry",
"grape",
"strawberry"
};
IEnumerable<string> query = fruits.Where(fruit => fruit.Length < 8);
// query contains: {apple,banana,mango,orange,grape}
IEnumerable<string> query2 = query.Where(fruit => fruits.Contains("apple"));
foreach (string fruity in query2)
{
Console.WriteLine(fruity);
}
// finally returns: {apple,banana,mango,orange,grape}
Therefore it seems as if input is returned if condition is true.
Kindly guide me if I’m wrong
Wherereturns a filtered sequence of the input for which the predicate returnedtrue. It is applied to each element in turn, and that item is either yielded or discarded. Basically:Look at the names:
That says, for every
fruit, see if the entire set (fruits, note the finals) returns an apple. The listfruitsdoes containapple, so that is true for every fruit.You possibly meant: