【问题标题】:OrderBy on List<string> propertyList<string> 属性上的 OrderBy
【发布时间】:2015-06-02 08:17:31
【问题描述】:

如何按以下方式对 List 类型的属性执行 OrderBy?

我想按列表中的名字和第一个字符串排序

UnOrdered : ResearcherNames

  • 佐伊·拉斯、乔·史密斯
  • 凯尔·伯特
  • 亚当·辛
  • 下令:ResearcherNames

  • 亚当·辛
  • 凯尔·伯特
  • 佐伊·拉斯,乔·史密斯
  • 这是我的班级的样子

    public class assignments
    {
        public List<string> ReseacherNames { get; set; }
        public List<string> ClientUserNames { get; set; }
        ------Other Properties ------
    }
    
     //fill list
     combinedAssignments = new List<assignments>();
     foreach(var item in db) 
     {
        combinedAssingments.Add(item)
     }
    
     //need to sort by ResearcherNames
     combinedAssignments = combinedAssignments.OrderBy(x => x.ReseacherNames ).ToList();
    

    当我使用上述 OrderBy 调用运行时,我得到“至少一个对象必须实现 IComparable”。错误。

    尝试

    所以我意识到我必须对 List 类型使用 IComparer 并实现了如下比较器:

     public class RecordComparer : IComparer<List<string>>
     {
            public int Compare(string x, string y)
            {
                if (x == null)
                {
                    if (y == null)
                    {
                        // If x is null and y is null, they're 
                        // equal.  
                        return 0;
                    }
                    else
                    {
                        // If x is null and y is not null, y 
                        // is greater.  
                        return -1;
                    }
                }
                else
                {
                    // If x is not null... 
                    // 
                    if (y == null)
                    // ...and y is null, x is greater.
                    {
                        return 1;
                    }
                    else
                    {
                        // ...and y is not null, compare the  
                        // lengths of the two strings. 
                        // 
                        int retval = x.Length.CompareTo(y.Length);
    
                        if (retval != 0)
                        {
                            // If the strings are not of equal length, 
                            // the longer string is greater. 
                            // 
                            return retval;
                        }
                        else
                        {
                            // If the strings are of equal length, 
                            // sort them with ordinary string comparison. 
                            // 
                            return x.CompareTo(y);
                        }
                    }
                }
            }
    
            public int Compare(List<string> x, List<string> y)
            {
                var result = 0;
                if ((x != null && x.Count>0 )&& ( y != null && y.Count>0) )
                {
    
                    return Compare(x[0], y[0]);
                }
    
                return result;
            }
        }
    }
    

    用法:

    var rc = new RecordComparer();
     combinedAssignments = combinedAssignments.OrderBy(x=>     x.ResearcherNames,rc).ToList();
    

    没有错误消息,但结果未按字母顺序排序。

    【问题讨论】:

    • 我很困惑 - 您是否尝试在对象中订购 ResearcherNamesResearcherNamesClientUserNames是什么关系?
    • ResearcherNames 和 ClientUserName 字段之间没有关系,只是碰巧显示该类中还有其他字段,例如 List。我想按这些 list 类型 对我的列表进行排序
    • 您的数据和预期输出显示您想要订购ReseracherNames (List&lt;string&gt;),但您的代码显示您正在尝试订购combinedAssignments (List&lt;assignments&gt;)。那么你真正想做的是哪一个?

    标签: linq sql-order-by


    【解决方案1】:

    不需要 IComparer。这将按照第一个研究人员的名字的顺序给你分配:

    combinedAssignments = combinedAssignments
      .OrderBy(x => x.ReseacherNames.FirstOrDefault())
      .ToList();
    

    【讨论】:

      猜你喜欢
      • 2013-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-04
      • 1970-01-01
      • 2023-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多