I have an IDictionary
I need to select the first Foo where the Bar.Prop1 matches a string value.
public class Foo { }
public class Bar
{
public String Prop1 { get; set; }
}
right now I have it like so…
foreach (var kvp in MyDictionary)
{
if (kvp.Value.Prop1 == theString)
{
var key = kvp.Key;
//Do something with it
break;
}
}
But that just doesn’t seem as clean as a LINQ Query is. ReSharper turned it into:
foreach (var kvp in MyDictionary.Where(kvp => kvp.Value.Prop1 == theString))
{
var key = kvp.Key;
//Do something with it
//break; is unnecessary because I only get one kvp back anyways.
}
I only want the very first item that matches, because I don’t ever expect to get back more than one KVP. That goes against the business logic, so Unit Testing takes care of that.
If this is the case, I would argue you need to use a stronger code guarantee of your intention, and that would be the
Single(orSingleOrDefault) method.Firstis going to return the first object of arbitrarily many that match a given predicate. If many goes against your expectations and business rules, this seems to be an error. Treat it as such.With
Single, if there is more than one matching item in a sequence, it will result in an exception.SingleOrDefaultallows for 0 or 1, but never more. If using this approach, you’d want to capture the result and compare to null before performing additional operations with it (firing methods, accessing properties, etc.).