reckless76
reckless76

Reputation: 122

Default Value when using SingleOrDefault()

So, if I have a simple class like:

class Color
{
    public int ID { get; set; }
    public string ColorName { get; set; }
}

and then create a collection of these objects:

List<Color> ColorList;

I'd like to then query this collection with LINQ.

Color selected = ColorList.SingleOrDefault(a => a.ID == someVariable);

My collection will have several colors, all with unique IDs, and one color that I'd want to be the default. What I'm looking for is the ability to specify what is returned when someVariable does not match any ID in the collection. Is this possible, or does SingleOrDefault only return null when a match isn't found?

Upvotes: 4

Views: 7786

Answers (6)

Nafiz Mahamud
Nafiz Mahamud

Reputation: 1

We can change dafault value by using a user created method when default value is empty or by using DefaultIfEmpty method.

int[] pageNumbers = { };

// Setting the default value to 1 after the query.
int pageNumber1 = pageNumbers.SingleOrDefault();
if (pageNumber1 == 0)
{
    pageNumber1 = 1;
}
Console.WriteLine("The value of the pageNumber1 variable is {0}", pageNumber1);

// Setting the default value to 1 by using DefaultIfEmpty() in the query.
int pageNumber2 = pageNumbers.DefaultIfEmpty(1).Single();
Console.WriteLine("The value of the pageNumber2 variable is {0}", pageNumber2);

/*
 This code produces the following output:

 The value of the pageNumber1 variable is 1
 The value of the pageNumber2 variable is 1
*/

Upvotes: 0

Zeus82
Zeus82

Reputation: 6375

Unfortunately SingleOrDefault will either return a single value of Default(T). The best you could do is something like this:

Color defaultColor;

var single = ColorList.SingleOrDefault(a => a.ID == someVariable);
single = selected == null ? defaultColor : single;

Upvotes: 0

user2160375
user2160375

Reputation:

SingleOrDefault returns default value of type only when matching instance is not found. In this case it will be null.

You can write own extension:

private static readonly Color defaultValue = new Color(/* params */);

public static Color SingleOrMyDefault(
          this IEnumerable<Color> source, 
           Func<Color, bool> predicate)
{
     return source.SingleOrDefault(predicate) ?? defaultValue;
}

usage:

var color = ColorList.SingleOrMyDefault(a => a.Id == 2);

Upvotes: 3

William Barbosa
William Barbosa

Reputation: 5005

You can try using null coalescing, since you can't specify a default value for a reference type:

Color selected = ColorList.SingleOrDefault(a => a.ID == someVariable) ?? DefaultValue;

Upvotes: 1

Habib
Habib

Reputation: 223322

Use ?? (null coalescing operator)

Color selected = ColorList.SingleOrDefault(a => a.ID == someVariable) ?? 
                          new Color {Id = 1234, ColorName = "Blue"}; //Default value

Upvotes: 1

p.s.w.g
p.s.w.g

Reputation: 149050

Because Color is a class, the default value is null. So you can just use the null-coalescing operator (??) like this:

Color selected = ColorList.SingleOrDefault(a => a.ID == someVariable) ?? defaultValue;

Upvotes: 16

Related Questions