I have a collection
List<KeyValuePair<string, Details>> x
where
public class Details
{
private int x;
private int y;
public Details()
{
x = 0;
y = 0;
}
...
}
I am using LINQ on my collection to return the Details instance where
x.Where(x => x.Key == aString).SingleOrNew().Value
Where .SingleOrNew is defined as
public static T SingleOrNew<T>(this IEnumerable<T> query) where T : new()
{
try
{
return query.Single();
}
catch (InvalidOperationException)
{
return new T();
}
}
So if a KeyValuePair<string, Details> is not found in the list x which satisfies the Where clause a new KeyValuePair<string, Details> is returned.
But the problem is the new KeyValuePair<string, Details> includes a null Details value.
When no match is found from the .Where clause I was wondering if any LINQ (extension) method can be used which would return a new KeyValuePair<string, Details> like SingleOrNew but where the .Value / Details part of the KeyValuePair has been initialised using the default Details parameterless constructor? So that its not null!