C# LINQ FirstOrDefault 检查默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18184166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINQ FirstOrDefault check for default value
提问by Raju Kumar
How can you check to see whether the object returned by the FirstOrDefault
LINQ function is in fact the default?
如何检查FirstOrDefault
LINQ 函数返回的对象是否实际上是默认对象?
For example:
例如:
Contact contact = dbo.contact
.Where(m => m.contactName == "Stackoverflow")
.FirstOrDefault();
Is there an alternative way to check whether the contact above is default value instead of using the following?
有没有其他方法可以检查上面的联系人是否是默认值而不是使用以下内容?
if (!contact.contactName.Equals("Stackoverflow"))
// do something
采纳答案by keyboardP
You wouldn't need to perform that equals check because your query only returns objects where the contantName is Stackoverflow
. When you use FirstOrDefault
it returns null
if no objects were found so you can do
您不需要执行等于检查,因为您的查询仅返回 contantName 为 的对象Stackoverflow
。当你使用FirstOrDefault
它时,null
如果没有找到对象,它就会返回,所以你可以这样做
if(contact == null)
do something
You know it's a reference type if Contact is a class so it's default value would be null. You can, however, check it's the default type of any object (reference or value) by using default
.
如果 Contact 是一个类,您就知道它是一个引用类型,因此它的默认值为 null。但是,您可以使用default
.
if(contact == default(Contact))
do something
As mentioned in the comments, you can possibly make your code more efficient by using the overload of FirstOrDefault
that takes a predicate.
正如评论中提到的,您可以通过使用FirstOrDefault
带谓词的重载来提高代码效率。
FirstOrDefault(m => m.contactName == "Stackoverflow")
You can also change the default value returned if your program needs to work with something other than a null
or 0
. For example
如果您的程序需要使用 anull
或以外的其他内容,您还可以更改返回的默认值0
。例如
Contact defaultContact = new Contact();
defaultContact.ContactName = "StackExchange";
Contact contact = dbo.contact.Where(m=>m.contactName == "Stackoverflow")
.DefaultIfEmpty(defaultContact).First();
The above will return the defaultContact
object if no other object was found (instead of returning null). If you do this then you don't need to check for null
or default(T)
because you know you have a Contact
object.
defaultContact
如果没有找到其他对象(而不是返回 null),上面的将返回该对象。如果你这样做,那么你不需要检查null
或者default(T)
因为你知道你有一个Contact
对象。
回答by Ryszard D?egan
FirstOrDefault
will return null
for reference types and default
for value types. Thus your test is invalid. In orther to check wheteher the value is default, you should compare it with default (Type)
:
FirstOrDefault
将返回null
引用类型和default
值类型。因此你的测试是无效的。为了检查该值是否为默认值,您应该将其与default (Type)
:
Contact contact = dbo.contact.FirstOrDefault(m => m.contactName == "Stackoverflow");
if (!object.Equals(contact, default (Contact)))
// Is not default
The code above will work with either struct Contact
or class Contact
. We also assume that default (Contact)
is never a valid return value of our query.
上面的代码适用于struct Contact
或class Contact
。我们还假设这default (Contact)
永远不是我们查询的有效返回值。
回答by Dhaval Patel
You can use
您可以使用
Contact contact = dbo.contact.where(m=>m.contactName!="" &&m.contactName!=null && m.contactName == "Stackoverflow").FirstOrDefault();
回答by vendettamit
FirstOrDefault() stands for first element, if not found then "Default" value of that type. It internally uses the default
keyword to return the default value.
FirstOrDefault() 代表第一个元素,如果未找到,则为该类型的“默认”值。它在内部使用default
关键字返回默认值。
usage: return default(Contact) or default(int)
用法:return default(Contact) or default(int)
for e.g. if it's a value type let say int
then it'll return 0. In case of reference types it'll return "Null
" etc.
例如,如果它是一个值类型,int
那么它会返回 0。在引用类型的情况下,它会返回“ Null
”等。
回答by Lev
Generally, FirstOrDefault
returns item which you've requested of default
value for collections type.
In case of references it's null
, in case of int - 0
etc.
通常,FirstOrDefault
返回您请求的default
集合类型值的项目。在引用的null
情况下,如果是 int -0
等。