C# 返回嵌套列表中的最小/最大编号
我正在尝试使用Linq在嵌套列表中查找最小/最大值 一个示例类是:C# 返回嵌套列表中的最小/最大编号,c#,linq,C#,Linq,我正在尝试使用Linq在嵌套列表中查找最小/最大值 一个示例类是: public class ListA { public HashSet<HashSet<int>> nestedList; public ListA() { nestedList = new HashSet<HashSet<int>>() { new HashSet<int> { 1, 3
public class ListA
{
public HashSet<HashSet<int>> nestedList;
public ListA()
{
nestedList = new HashSet<HashSet<int>>()
{
new HashSet<int> { 1, 3, 5, 7 },
new HashSet<int> { 2, 12, 7, 19 },
new HashSet<int> { 6, 9 , 3, 14 }
};
}
public int MaxNumber
{
// return the highest number in list
}
}
公共类列表a
{
公共哈希集嵌套列表;
公共列表a()
{
nestedList=newhashset()
{
新哈希集{1,3,5,7},
新哈希集{2,12,7,19},
新哈希集{6,9,3,14}
};
}
公共整数最大值
{
//返回列表中的最大数字
}
}
在上面的示例中,最小值为1,最大值为19
我正在努力获取任何东西以获得有效的语法。有人帮忙吗
nestedList.SelectMany(item => item).Max();
及
及
而Max
很可能会产生您想要的结果
也考虑使用<代码> DefaultIfEmpty < /代码>(用任何默认值对上下文都有意义)-这将确保<代码>马克斯< /> >如果<代码> NestistdList为空,则不会引发异常。
而Max
很可能会产生您想要的结果
也考虑使用<代码> DefaultIfEmpty < /代码>(用任何默认值对上下文都有意义)-这将确保<代码>马克斯< /> >如果<代码> NestistdList为空,则不会引发异常。
请阅读并显示您的尝试。的副本。请阅读并显示您的尝试。的副本。多么尴尬-我尝试在选择之后使用第二个选择多-谢谢。多么尴尬-我尝试在选择之后使用第二个选择多-谢谢。
nestedList.SelectMany(item => item).Min();
public int MaxNumber
{
get { return nestedList.SelectMany(z => z).DefaultIfEmpty(0).Max(); }
}