OrderBy在List <string>属性上

时间:2015-06-01 20:13:22

标签: linq sql-order-by

如何以下列方式为List类型属性执行OrderBy?

我想按照名字和名单中的第一个字符串排序

UnOrdered:ResearcherNames

  • Zoe Las,Joe Smith
  • Kyle Burt
  • Adam Zing
  • Ordered:ResearcherNames

  • Adam Zing
  • Kyle Burt
  • Zoe Las,Joe Smith
  • 以下是我班级的样子

    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”。错误。

    尝试

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

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

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

    1 个答案:

    答案 0 :(得分:0)

    不需要IComparer。这将按照第一个研究人员的名字顺序为您提供作业:

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