嗨,我有个ENUM
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然后我有一个人,例如,他有一些这样的教育,例如,他有三个人: High_School_Incomplete High_School_Complete Secondary_Technical_Or_Vocational
我希望从这个例子中获得最高值: Secondary_Technical_Or_Vocational。
例如,result.degree是Enum元素,它必须得到我想要的enum值。applicant.Educations是教育的列表。每个元素都有1度元素。对每种原料进行比较,得到最高的枚举元素度。
result.Degree = applicant.Educations.Where(x => (int)x.Degree)发布于 2019-03-01 09:07:27
您可以使用Max获得最大程度。要将其赋值给result,需要将其转换回枚举类型:
result.Degree = (NameOfYourEnum)list.Max(x => x.Degree);发布于 2019-03-01 09:07:49
最简单的方法是对学位进行排序,然后取第一个。
result.Degree = applicant.Educations.OrderByDescending(x => x.Degree).FirstOrDefault();编辑:忘记了Max.That更好。
发布于 2019-03-01 09:12:38
问题不太清楚,所以我作了一些假设。它看起来像你所说的一个人,有一个教育的列表,这些教育应该是一个学位的列表,每个都有一个枚举值。
您希望从该列表中获得最高值,并将其转换为枚举值。
因此,通过这样的代码设置:
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);请注意,我是如何从列表中提取最高教育的,然后如果需要的话,我可以将其解析回其枚举值。
https://stackoverflow.com/questions/54940916
复制相似问题