-2

So i have this function that return element from collection based on condition

public static T Search<T>(IEnumerable<T> source, Func<T, bool> filter)
{
    return source.FirstOrDefault(filter);
}

And i want to convert this to return all the elements form my collection that mach my condition.

so instead of change the function signature to public static IEnumerable<T> Search<T>(IEnumerable<T> source, Func<T, bool> filter)

What i need to changed inside my function ?

danny kob
  • 171
  • 1
  • 2
  • 12

2 Answers2

5

Use Where instead of FirstOrDefault

public static IEnumerable<T> Search<T>(IEnumerable<T> source, Func<T, bool> filter)
{
    return source.Where(filter);
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

Use the Where method instead of FirstOrDefault

Jonas Høgh
  • 10,358
  • 1
  • 26
  • 46