FirstOrDefault: Default Value Other Than Null
FirstOrDefault: Default Value Other Than Null
FirstOrDefault: Default Value Other Than Null
PUBLIC
Stack Overflow
Tags
FirstOrDefault: Default value other than null Ask Question
Users
2 I've been looking for something like the above comment for quite a while, it
helped immensely. This should be the accepted answer. – Brandon Jan 10 '14 at
16:28
The above comment is the best answer. – Tom Padilla May 19 '14 at 14:45
11 Answers
Again, this can't really tell if there was anything in your sequence, or if the
first value was the default.
although as Mr. Steak points out this could be done just as well by
.DefaultIfEmpty(...).First() .
Your generic methods need <T> in their names, but more serious is that value
== default(T) doesn't work (because who knows if T can be compared for
equality?) – AakashM Oct 19 '12 at 10:54
Thanks for pointing that out, @AakashM; I've actually tried this now and I think it
should be OK (although I don't like the boxing for value types). – Rawling Oct 19
'12 at 10:57
No. Or rather, it always returns the default value for the element type...
which is either a null reference, the null value of a nullable value type, or the
natural "all zeroes" value for a non-nullable value type.
Is there any particular way that this can be set up so that if there is no
value for a particular query some predefined value is returned as the
default value?
Of course this will also give you the "other default value" if the first value is
present, but is a null reference...
11 Works for me better than the accepted answer! – Jocie Jan 23 '14 at 15:42
I know the question asks for reference type but your solution doesn't work for
when elements are value types like int . I much prefer the use of
DefaultIfEmpty :
src.Where(filter).DefaultIfEmpty(defaultValue).First() . Works for both
value type and reference type. – KFL Feb 16 at 21:41
@KFL: For non-nullable value types I'd probably use that too - but it's more long-
winded for nullable cases. – Jon Skeet Feb 16 at 21:43
Awesome control over return types when default is null .. :) – Sundara Prabu Feb
17 at 9:53
You can use DefaultIfEmpty followed by First:
T customDefault = ...;
IEnumerable<T> mySequence = ...;
mySequence.DefaultIfEmpty(customDefault).First();
I love the idea of DefaultIfEmpty - it works with ALL APIs that need a default to
be specified: First() , Last() , etc. As a user, you don't need to remember
which APIs allow to specify default which don't. Very elegant! – KFL Feb 16 at
21:45
the default keyword, which will return null for reference types and zero
for numeric value types. For structs, it will return each member of the
struct initialized to zero or null depending on whether they are value or
reference types. For nullable value types, default returns a
System.Nullable, which is initialized like any struct.
Therefore, the default value can be null or 0 depending on whether the type
is a reference or value type, but you cannot control the default behaviour.
Example:
1 Note that DefaultIfEmpty returns the default IF the collection is empty (has 0
items). If you use First WITH a matching expression like in your example and
that condition doesn't find any item your return value will be empty. – OriolBG Mar
10 '17 at 12:17
C# older version:
The null propagation operator is not allowed in expression tree lamba's. – Lars335
Oct 23 '17 at 1:20
2 The only difference between this answer and Vitamin C's answer is that this one
uses FirstOrDefault instead of First . According to msdn.microsoft.com/en-
us/library/bb340482.aspx, the recommended usage is First – Daniel Sep 9 '15
at 21:43
I just had a similar situation and was looking for a solution that allows me to
return an alternative default value without taking care of it at the caller side
every time I need it. What we usually do in case Linq does not support what
we want, is to write a new extension that takes care of it. That´s what I did.
Here is what I came up with (not tested though):
I know its been a while but Ill add to this, based on the most popular answer
but with a little extension Id like to share the below:
This allows me to call it inline as such with my own example I was having
issues with:
When you think that it was helpful, you can upvote. This is not an answer. –
jannagy02 Apr 6 '17 at 9:19