How to return empty string if an object is null?
Solution 1
You can use DefaultIfEmpty
with an argument to specify the fallback value:
var link = socials
.Where(p => p.type == Facebook)
.Select(p => p.Url)
.DefaultIfEmpty("")
.First();
FirstOrDefault
is not necessary anymore, it is safe to use First
with DefaultIfEmpty
.
Solution 2
Another option is to use null coalesce operator
var link = (socials
.Where(p => p.type == Facebook)
.Select(p => p.Url)
.FirstOrDefault()) ?? string.empty;
I understood that string.empty
is preferred over ""
but that may not be the case - see comment below.
UPDATE In C# 6 there is a null conditional operator (also known as the "Elvis" operator):
var link = socials
.Where(p => p.type == Facebook)
.Select(p => p.Url)
.FirstOrDefault()?.URL ?? string.empty;
A simple example:
stringbuilder sb = null;
// do work
string s = sb?.ToString() ?? "no values found";
Solution 3
Using C# 6 you can use a null-conditional operator (?.
) to return the URL if the object returned from the LINQ statement isn't null
, i.e. if a value was found, or null
otherwsie.
var link = socials.Where(p => p.type == Facebook).FirstOrDefault()?.URL;
If you then want to change a null
value to an empty string or a custom string then use the null-coalescing operator (??
).
var link = socials.Where(p => p.type == Facebook).FirstOrDefault()?.URL ?? "No URL";
It should be noted that this won't make any distinction between whether an object wasn't found or an object was found but had a null
URL value.
Related videos on Youtube
markzzz
Updated on June 15, 2022Comments
-
markzzz almost 2 years
Here is a snippet of my code:
var link = socials.Where(p => p.type == Facebook).FirstOrDefault().URL;
the problem is that there aren't any social object in the list,
FirstOrDefault()
returnnull
, and.URL
trigger an exception.How can I avoid this in "one" line with LINQ? Thus, if null return empty
""
(or custom even better) string, else.URL
?-
Sirwan Afifi over 8 yearsyou can use .First();
-
Tim Schmelter over 8 years@SirwanAfifi: which causes an exception if there is no type==facebook.
-
-
Tim Schmelter over 8 yearsWhy is
string.empty
preferred over""
? Tha's just a matter of personal preferences. Imo""
is very clear. -
Tim Schmelter over 8 yearsThere's a subtle difference between yours and mine answer. You get
String.Empty
if there is notype==facebook
and also if the first matchingUrl
isnull
. My approach still yieldsnull
in that case. -
Steve over 8 years
-
markzzz over 8 yearsBut DefaultIfEmpty works even if the Select return 0 object? I mean, if the where count is 0, it returns at least one ""?
-
Tim Schmelter over 8 yearsYes, that's the sole purpose of
DefaultIfEmpty
. It comes only into play if the sequence contains no elements. Then it uses the default value of the type or - if you use the overload as in my answer - it uses the value provided. So with the same result as if the sequence contained only one element with that value. -
Sagar Khatri about 5 yearsSaved few additional lines of code and time. Works as expected