我有一个收藏
List<KeyValuePair<string, Details>> x
在哪里
public class Details
{
private int x;
private int y;
public Details()
{
x = 0;
y = 0;
}
...
}
我在我的集合上使用 LINQ 来返回Details
实例
x.Where(x => x.Key == aString).SingleOrNew().Value
其中.SingleOrNew
定义为
public static T SingleOrNew<T>(this IEnumerable<T> query) where T : new()
{
try
{
return query.Single();
}
catch (InvalidOperationException)
{
return new T();
}
}
因此,如果在满足子句 aKeyValuePair<string, Details>
的列表中找不到a ,则返回。x
Where
new KeyValuePair<string, Details>
但问题是new KeyValuePair<string, Details>
包含一个空的详细信息值。
当从子句中找不到匹配项时,.Where
我想知道是否可以使用任何 LINQ(扩展)方法,它会返回一个new KeyValuePair<string, Details>
like SingleOrNew
,但是.Value
/Details
的部分KeyValuePair
已使用默认的无Details
参数构造函数初始化?所以它不为空!