【问题标题】:Trying to find the top 3 properties of a POCO instance试图找到 POCO 实例的前 3 个属性
【发布时间】:2010-11-15 08:23:48
【问题描述】:

我有一个包含学生分数的简单 POCO 类。

For example:
Math - 83%
Engrish - 82%
Chemistry - 81%
Drama - 100%
etc..

有没有办法(使用LINQ?)我可以找出按分数排序的前 3 个属性?

我假设最终对象是匿名类型的 IList,它将有两个字段。

  1. 名称(属性的名称)
  2. 分数(十进制值)。

虽然对象中的属性数量是有限的 :)

有什么建议吗?

作为替代答案,这可以在数据库中完成吗?

【问题讨论】:

    标签: .net tsql poco


    【解决方案1】:

    使用以主题为键,分数为值的字典会更简单:

    Dictionary<string, int> scores = new Dictionary<string, int>();
    ...
    
    var top3Subjects = (from s in scores
                        orderby s.Value descending
                        select s).Take(3);
    

    这会返回一个IEnumerable&lt;KeyValuePair&lt;string, int&gt;&gt;,您可以像这样使用它:

    foreach (var s in top3Subjects)
    {
        Console.WriteLine("{0} : {1}", s.Key, s.Value);
    }
    

    【讨论】:

    • 实际上,这不是一个坏建议。而且它很好很简单:)
    【解决方案2】:

    除非您已经在内存中拥有所有数据,否则让数据库为您选择正确的数据会更有效。

    如果将成绩作为字段存储在数据库中,则必须对其进行规范化以使其可以查询。最好的办法是重新设计数据库并将成绩作为行放在单独的表中。数据应该在表格的字段中,而不是作为字段名称:

    select top 3 GradeName, Grade
    from Grades
    where StudentId = 42
    order by Grade desc
    

    您也可以即时对数据进行标准化,但这当然效率不高:

    select top 3 GradeName, Grade
    from (
       select GradeName = 'Engrish', Grade = Engrish from Students where StudentId = 42
       union all
       select 'Drama', Drama from Students where StudentId = 42
       union all
       select 'Math', Math from Students where StudentId = 42
       union all
       select 'Chemistry', Chemistry from Students where StudentId = 42
    ) Grades
    order by Grade desc
    

    【讨论】:

    • 我也喜欢在数据库中这样做。
    【解决方案3】:

    您的问题不清楚分数是所有单独的属性还是某种列表。如果它们是一个列表,这将起作用:

     var topScores =
        (from s in Scores
        orderby s.Score descending
        select new { s.Name, s.Score}).Take(3);
    

    【讨论】:

    • 嗨乔恩。我确定我提到它们是独立的属性。因此,如果我没有适当地沟通,请道歉。不过感谢您的回答:)(我 lub linq):)
    【解决方案4】:

    你在寻找这样的东西吗?

    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);
        }
    }
    

    【讨论】:

    • 我最终调用了 .ToDictionary :) 简单 :)
    猜你喜欢
    • 2010-11-22
    • 1970-01-01
    • 2021-01-02
    • 2020-06-02
    • 2020-09-02
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多