Norgul
Norgul

Reputation: 4783

C# dictionary where clause fail

I have a dictionary of key-value pairs. How can I check for where clause failure?

SomeDictionary.Where(x => x.Value && someOtherBool).First();

Is there a way to check if this returned some result without surrounding with try-catch block? Sure, I can call FirstOrDefault() but I don't get what I need then...

There should really be a method FirstOrReturn(arg)...

Upvotes: 1

Views: 173

Answers (1)

fubo
fubo

Reputation: 45947

FirstOrReturn(arg) should be

.Where(x => x.Value && someOtherBool).DefaultIfEmpty(arg).First();

Upvotes: 7

Related Questions