尝试查找POCO实例的前3个属性 [英] Trying to find the top 3 properties of a POCO instance
本文介绍了尝试查找POCO实例的前3个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的POCO课程,其中包含学生的分数.
I have a simple POCO class that contains the student's scores.
For example:
Math - 83%
Engrish - 82%
Chemistry - 81%
Drama - 100%
etc..
有没有办法(使用 LINQ ?)我可以确定前3名按得分排序的属性?
Is there a way (using LINQ?) that I could figure out the top 3 properties ordered by score?
我假设最终对象将是匿名类型的IList< T>,它将具有两个字段.
I'm assuming the final object will be an IList<T> of an anonymous type, which will have two fields.
- 名称(属性名称)
- 分数(十进制值).
对象中的属性数量虽然是有限的:)
The number of properties in the object ARE finite though :)
有什么建议吗?
作为替代答案,可以在数据库中代替吗?
As an alternative answer, could this be done in a database instead?
推荐答案
您是否正在寻找类似的东西?
Are you looking for something like this?
class Notes
{
public double Math{ get; set; }
public double English { get; set; }
public double Chemistry { get; set; }
public double Drama { get; set; }
public string IgnoreMePlease { get; set; }
}
class Program
{
static void PrintHighestNotes(Notes notes)
{
var pairs = from property in notes.GetType().GetProperties()
where property.PropertyType == typeof (double)
select new
{
Name = property.Name,
Value = (double) property.GetValue(notes, null)
};
var result = pairs.OrderByDescending(pair => pair.Value);
foreach (var pair in result)
Console.WriteLine("{0} = {1}", pair.Name, pair.Value);
}
static void Main(string[] args)
{
Notes notes = new Notes()
{
Chemistry = 0.10,
Math = 0.2,
Drama = 1,
English = 0.3,
IgnoreMePlease = "Ignore"
};
PrintHighestNotes(notes);
}
}
这篇关于尝试查找POCO实例的前3个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文