I have a dictionary of type
Dictionary
I want to return the first instance where a condition is met using
var
What you want is an Any
method that gives you the matching element as well. You can easily write this method yourself.
public static class IEnumerableExtensions
{
public static bool TryGetFirst(this IEnumerable source,
Func predicate,
out TSource first)
{
foreach (TSource item in source)
{
if (predicate(item))
{
first = item;
return true;
}
}
first = default(TSource);
return false;
}
}