What is the difference between Find and where RRS feed

  • Question

  • 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");
                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?

    i understand this.

    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().

    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?


    • Edited by Sudip_inn Thursday, December 21, 2017 10:33 AM
    Thursday, December 21, 2017 10:31 AM

All replies

  • Hi Sudip_inn,

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

    The Find method on DbSet uses the primary key value to attempt to find an entity tracked by the context. If the entity is not found in the context then a query will be sent to the database to find the entity there. Null is returned if the entity is not found in the context or in the database. so you could not use the following the following code.

    var emp = oList.Find(e => e.Name == "John");

    You could use find method like this:

    var emp = oList.Find(1);

    But where method, you could use all of condition.

    For more information, please refer to:


    Best regards,

    Zhanglong Wu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 22, 2017 5:08 AM
  • sorry not clear the difference between find and where. please come with nice example to explain it.

    what is the meaning of this code

    var emp = oList.Find(1);

    does it return the first record from the list?

    i guess the above code not finding employee by any field name.

    see this code

    var blog = context.Blogs.Find(3);

    does the above code find records whose blogid is 3?

    how find can understand which fields is pk in db table because table may not have a

    any pk column...then what will happen and how find will work?

    • Edited by Sudip_inn Thursday, January 4, 2018 11:54 AM
    Thursday, January 4, 2018 11:45 AM