[英]What do I use in place of “FirstOrDefault()” if I want to find an exact search result in my database?
I am currently using the following code within my controller: 我目前在我的控制器中使用以下代码:
Instructor instructor = db.Instructors.FirstOrDefault(
o => o.UserName == User.Identity.Name);
to select someone by username. 用户名选择某人。 My understanding is that I will have trouble using "FirstOrDefault()" if I have user's with similar user names (ie searching for "MrUser" when I have users named "MrUserOne" "MrUserTwo" and "MrUser" may yield "MrUserOne" because it was the 'first' search result to show up), If I am correct in my understanding of future difficulties with "FirstOrDefault," what should I use in it's place?
我的理解是,如果我的用户具有相似的用户名,我将无法使用“FirstOrDefault()”(当我有名为“MrUserOne”的用户搜索“MrUser”时,“MrUserTwo”和“MrUser”可能会产生“MrUserOne”,因为这是“第一个”出现的搜索结果),如果我对“FirstOrDefault”未来遇到的困难的理解是正确的,我应该在它的位置使用什么?
Or am I wrong in my understanding of how FirstOrDefault will work? 或者我对FirstOrDefault如何运作有所了解?
It appears you don't quite understand how the ==
operator works in this scenario. 您似乎不太了解
==
运算符在此方案中的工作原理。 Assuming the UserName
and Name
values are both string
then ==
will do an exact ordinal match. 假设
UserName
和Name
值都是string
那么==
将进行精确的序数匹配。 The name "MrUser"
won't match "MrUserOne"
at all. "MrUser"
这个名字"MrUser"
不会与"MrUserOne"
匹配。 It will only match "MrUser"
. 它只会匹配
"MrUser"
。
That being said this code will return the first user whose Name
value exactly matches o.UserName
or null
if none do. 也就是说,此代码将返回其
Name
值与o.UserName
完全匹配的第一个用户,如果没有,则返回null
。
JaredPar is quite correct in what he's saying. JaredPar说的很正确。 But you can also use SingleOrDefault if what you're expecting is to be exactly one result.
但是如果你期望的是一个结果,你也可以使用SingleOrDefault。 You can read more about that in this Stackoverflow question: LINQ: When to use SingleOrDefault vs. FirstOrDefault() with filtering criteria
您可以在此Stackoverflow问题中阅读更多相关信息: LINQ:何时使用SingleOrDefault与FirstOrDefault()一起使用过滤条件
When you use ==, you're already doing an exact search result. 当您使用==时,您已经在进行精确的搜索结果。 The expression
表达方式
o => o.UserName == User.Identity.Name
is translated to 被翻译成
WHERE UserName = [Username]
on the database side. 在数据库方面。 This is not to be confused with a statement like
这不要与像这样的陈述相混淆
o => o.UserName.Contains(User.Identity.Name)
which would translate to 这将转化为
WHERE UserName like '%[Username]%'
See http://msdn.microsoft.com/en-us/library/bb738681.aspx for how string functions are translated for Linq to Entities. 有关如何将Linq转换为实体的字符串函数,请参见http://msdn.microsoft.com/en-us/library/bb738681.aspx 。
That being said, your FirstOrDefault will give you the first name that exactly matches the username you provided. 话虽这么说,您的FirstOrDefault将为您提供与您提供的用户名完全匹配的名字。 So your example would match the first user with the name "MrUser".
因此,您的示例将匹配第一个用户名为“MrUser”的用户。
Your query will just return the first user whose UserName
exactly matches User.Identity.Name
(it will not match users that start with User.Identity.Name
, as you seem to be saying). 您的查询将返回其
UserName
与User.Identity.Name
完全匹配的第一个用户(它不会与以User.Identity.Name
开头的用户匹配,正如您似乎所说的那样)。
So if there are 2 users with identical names, the return value will just be the first one. 因此,如果有2个用户具有相同的名称,则返回值将只是第一个。 And the "first" one might be different, depending on how the database feels on returning the results to you (since you are not specifying an order).
而“第一个”可能会有所不同,具体取决于数据库在将结果返回给您时的感受(因为您没有指定订单)。
As a side note, you should get familiar with these 4 similar methods, they are worth knowing: 作为旁注,您应该熟悉这4种类似的方法,它们值得了解:
>1 result 0 results
First() return 1st throw
FirstOrDefault() return 1st return null
Single() throw throw
SingleOrDefault() throw return null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.