嗨,我有一个像
这样的枚举Elementary_Education = 1,
High_School_Incomplete = 2,
High_School_Complete = 3,
Secondary_Technical_Or_Vocational = 5,
Vocational_Education_Student = 7,
Higher_Education_Institution__Student = 9
然后我有一个这样的人,例如他受过一些这样的教育 这三个: 高中_不完整 高中_完成 Secondary_Technical_Or_Vocational
我想要的是从本例中获得3的最大值: Secondary_Technical_Or_Vocational。
例如result.degree是Enum元素,它必须获取我想要的枚举的值。 教育是教育清单。每个都有1度元素。 想要比较每个原始元素并获得最高的枚举元素度。
result.Degree = applicant.Educations.Where(x => (int)x.Degree)
答案 0 :(得分:1)
您可以使用Max
获得最高学位。要分配给result
,您需要将其强制转换回枚举类型:
result.Degree = (NameOfYourEnum)list.Max(x => x.Degree);
答案 1 :(得分:0)
最简单的方法是对学位进行排序,然后取第一个。
result.Degree = applicant.Educations.OrderByDescending(x => x.Degree).FirstOrDefault();
编辑:忘记了Max,那就更好了。
答案 2 :(得分:0)
这个问题不是很清楚,所以我做了一些假设。看起来像您所说的一个人,有一系列的教育,这可能是一个学位列表,每个学位都有一个枚举值
您希望从该列表中获取最高价值并将其转换为枚举值。
因此具有这样的代码设置:
public class Person
{
public List<Education> Educations = new List<Education>();
}
public class Education
{
public Enums.DegreeType Degree { get; set; }
}
public class Enums
{
public enum DegreeType
{
Elementary_Education = 1,
High_School_Incomplete = 2,
High_School_Complete = 3,
Secondary_Technical_Or_Vocational = 5,
Vocational_Education_Student = 7,
Higher_Education_Institution__Student = 9
}
}
我们现在可以执行以下操作:
var person = new Person();
person.Educations.Add(new Education { Degree = Enums.DegreeType.High_School_Complete });
person.Educations.Add(new Education { Degree = Enums.DegreeType.Vocational_Education_Student });
var highestEd = person.Educations.Select(p => (int)p.Degree).Max();
Enums.DegreeType enumHighest;
Enum.TryParse(highestEd.ToString(), out enumHighest);
请注意如何从列表中提取最高学历,然后根据需要将其解析回其枚举值。