我到处搜索,并没有真正找到一个明确的答案,什么时候你想使用. first,什么时候你想使用. firstordefault与LINQ。

什么时候你想用。first ?只有当你想捕捉异常,如果没有结果返回哪里? var result =列表。Where(x => x == "foo").First(); 什么时候使用。firstordefault ?当你总是想默认类型,如果没有结果? var result =列表。Where(x => x == "foo").FirstOrDefault(); 说到这,那塔克呢? var result =列表。Where(x => x == "foo").Take(1);


当前回答

Others have very well described the difference between First() and FirstOrDefault(). I want to take a further step in interpreting the semantics of these methods. In my opinion FirstOrDefault is being overused a lot. In the majority of the cases when you’re filtering data you would either expect to get back a collection of elements matching the logical condition or a single unique element by its unique identifier – such as a user, book, post etc... That’s why we can even get as far as saying that FirstOrDefault() is a code smell not because there is something wrong with it but because it’s being used way too often. This blog post explores the topic in details. IMO most of the times SingleOrDefault() is a much better alternative so watch out for this mistake and make sure you use the most appropriate method that clearly represents your contract and expectations.

其他回答

第一:

返回序列的第一个元素 抛出异常:结果中没有元素 使用when:当期望有多个元素,而您只想要第一个元素时

FirstOrDefault:

返回序列的第一个元素,如果没有找到元素,则返回默认值 抛出异常:仅当源为空时 使用when:当期望有多个元素,而您只想要第一个元素时。同样,结果为空也是可以的

来自:http://www.technicaloverload.com/linq-single-vs-singleordefault-vs-first-vs-firstordefault/

第()

返回序列的第一个元素。 当结果中没有元素或源为空时抛出错误。 如果需要多个元素,而你只需要第一个元素,你应该使用它。

FirstOrDefault ()

返回序列的第一个元素,如果没有找到元素,则返回默认值。 仅当源为空时才抛出错误。 如果需要多个元素,而你只需要第一个元素,你应该使用它。 如果结果为空也很好。

我们有一个UserInfos表,其中有如下所示的一些记录。根据下面的表格,我创建了一个例子…

如何使用First()

var result = dc.UserInfos.First(x => x.ID == 1);

只有一条记录ID== 1。应该返回这个记录 ID: 1姓:Manish姓:Dubey邮箱:xyz@xyz.com

var result = dc.UserInfos.First(x => x.FName == "Rahul");   

有多个FName == "Rahul"的记录。第一个记录应该返回。 ID: 7名:Rahul姓:Sharma邮箱:xyz1@xyz.com

var result = dc.UserInfos.First(x => x.ID ==13);

没有ID== 13的记录。应该会发生错误。 InvalidOperationException:序列不包含元素

如何使用FirstOrDefault()

var result = dc.UserInfos.FirstOrDefault(x => x.ID == 1);

只有一条记录ID== 1。应该返回这个记录 ID: 1姓:Manish姓:Dubey邮箱:xyz@xyz.com

var result = dc.UserInfos.FirstOrDefault(x => x.FName == "Rahul");

有多个FName == "Rahul"的记录。第一个记录应该返回。 ID: 7名:Rahul姓:Sharma邮箱:xyz1@xyz.com

var result = dc.UserInfos.FirstOrDefault(x => x.ID ==13);

没有ID== 13的记录。返回值为空

希望它能帮助你理解何时使用First()或FirstOrDefault()。

另一个需要注意的区别是,如果您在生产环境中调试应用程序,您可能无法访问行号,因此识别方法中哪个特定的. first()语句抛出异常可能很困难。

异常消息也不包括您可能使用过的任何Lambda表达式,这会使任何问题更难调试。

这就是为什么我总是使用FirstOrDefault(),即使我知道空条目将构成异常情况。

var customer = context.Customers.FirstOrDefault(i => i.Id == customerId);
if (customer == null)
{
   throw new Exception(string.Format("Can't find customer {0}.", customerId));
}

当我知道或期望序列至少有一个元素时,我会使用First()。换句话说,当出现异常时,序列为空。

当您知道需要检查是否存在元素时,请使用FirstOrDefault()。换句话说,当序列为空是合法的时候。您不应该依赖异常处理进行检查。(这是不好的做法,可能会影响性能)。

最后,First()和Take(1)之间的区别是First()返回元素本身,而Take(1)返回只包含一个元素的元素序列。

好吧,让我说说我的意见。 First / Firstordefault用于使用第二个构造函数。我不会解释它是什么,但它是指你可能总是使用一个,因为你不想引起异常。

person = tmp.FirstOrDefault(new Func<Person, bool>((p) =>
{
    return string.IsNullOrEmpty(p.Relationship);
}));