Find和where之间有什么区别 [英] What is the difference between Find and where

查看:788
本文介绍了Find和where之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


查看我的代码

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 ?

何时使用find以及何时使用where?

when to use find and when to use where?

我理解这一点。

最大的区别是 oList.Where(e => e.Name ==" John")是IEnumerable< Employee>的类型,但
oList.Find(e => e.Name ==" John")是一种Employee。在这个演示中(我的意思是在这种情况下),
oList.Find(e => e.Name ==" John")等于 oList.Where(e => e.Name ==" John")。FirstOrDefault()

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");和var emp = oList.Find(e => e.Name ==" John");都返回员工数据。那么有什么不同呢?

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?

谢谢

推荐答案

嗨Sudip_inn,

Hi Sudip_inn,

>>查找并返回两者的数据。然后有什么区别?

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

DbSet上的Find方法使用主键值来尝试查找上下文跟踪的实体。如果在上下文中找不到该实体,则将向数据库发送查询以在那里找到该实体。如果在上下文或数据库中找不到实体
,则返回Null。所以你不能使用下面的代码。

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

你可以使用这样的find方法:

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:

https://msdn.microsoft .com / zh-CN / library / jj573936%28v = vs.113%29.aspx?f = 255& MSPPError = -2147217396

祝你好运,

Zhanglong Wu

Zhanglong Wu


这篇关于Find和where之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆