27

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() return null, 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?

2
  • you can use .First(); Commented Aug 21, 2015 at 10:28
  • 9
    @SirwanAfifi: which causes an exception if there is no type==facebook. Commented Aug 21, 2015 at 10:30

3 Answers 3

48

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.

Sign up to request clarification or add additional context in comments.

3 Comments

But DefaultIfEmpty works even if the Select return 0 object? I mean, if the where count is 0, it returns at least one ""?
Yes, 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.
Saved few additional lines of code and time. Works as expected
17

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"; 

3 Comments

Why is string.empty preferred over ""? Tha's just a matter of personal preferences. Imo "" is very clear.
There's a subtle difference between yours and mine answer. You get String.Empty if there is no type==facebook and also if the first matching Url is null. My approach still yields null in that case.
6

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.