locked
What is the difference between Find and where RRS feed

  • Question

  • User1183902823 posted

    see my code

                List<Employee> oList = new List<Employee>
                {
                    new Employee{ID=1,Name="John"},
                    new Employee{ID=2,Name="Jacob"},
                    new Employee{ID=3,Name="Lisa"},
                    new Employee{ID=4,Name="Rony"},
                };
    
                var emp = oList.Where(e => e.Name == "John");
    
                OR
    
                var emp = oList.Find(e => e.Name == "John");

    find and where both return data. then what is the difference ?

    when to use find and when to use where?

    thanks

    Thursday, December 14, 2017 5:30 PM

Answers

  • User475983607 posted

    Find came out with Generics back in .NET 2.0 and is a member of List(T).

    https://msdn.microsoft.com/en-us/library/x0b5b5bc(v=vs.110).aspx

    Where is an extension method of the Enumerable type that comes with Linq.

    https://msdn.microsoft.com/en-us/library/bb534803(v=vs.110).aspx

    You'll need to use Where when querying an IEnumerable<T> type as Find() is not available.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 14, 2017 8:07 PM
  • User1400794712 posted

    Hi tridip1974,

    The biggest difference is that oList.Where(e => e.Name == "John") is type of IEnumerable<Employee>, but oList.Find(e => e.Name == "John") is a type of Employee. In this demo(I just mean in this case), oList.Find(e => e.Name == "John") is equal to oList.Where(e => e.Name == "John").FirstOrDefault().

    Then just like what mgebhard said, Find() can only apply to List(T), then Where() is applicable against multiple kinds of sequences, including List<T>, T[], Collection<T>, etc. Any sequence that implements IEnumerable<T> can use Where() method.

    Best Regards,

    Daisy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 15, 2017 6:17 AM

All replies

  • User475983607 posted

    Find came out with Generics back in .NET 2.0 and is a member of List(T).

    https://msdn.microsoft.com/en-us/library/x0b5b5bc(v=vs.110).aspx

    Where is an extension method of the Enumerable type that comes with Linq.

    https://msdn.microsoft.com/en-us/library/bb534803(v=vs.110).aspx

    You'll need to use Where when querying an IEnumerable<T> type as Find() is not available.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 14, 2017 8:07 PM
  • User1400794712 posted

    Hi tridip1974,

    The biggest difference is that oList.Where(e => e.Name == "John") is type of IEnumerable<Employee>, but oList.Find(e => e.Name == "John") is a type of Employee. In this demo(I just mean in this case), oList.Find(e => e.Name == "John") is equal to oList.Where(e => e.Name == "John").FirstOrDefault().

    Then just like what mgebhard said, Find() can only apply to List(T), then Where() is applicable against multiple kinds of sequences, including List<T>, T[], Collection<T>, etc. Any sequence that implements IEnumerable<T> can use Where() method.

    Best Regards,

    Daisy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 15, 2017 6:17 AM
  • User1183902823 posted

    var emp = oList.Where(e => e.Name == "John"); and var emp = oList.Find(e => e.Name == "John"); both return employee data. so what is the difference in it?

    Friday, December 15, 2017 11:19 AM