如何按照一定的规则组织馆藏?

时间:2018-09-24 08:29:07

标签: c# linq

我上课:

 public class ClassA
 {
   public String ClassName {get;set;}
   public int ClassId {get;set;}
   public Guid UID {get;set}
  }

“ ClassName”可能是:A,B,C,D和“ ClassId”:10、20、30、40;

无序集合:

List<ClassA> unorderedCollection=GetCollection();

我想按以下顺序组织集合:

  1. A
  2. B
  3. C
  4. D

或来自ClassId

  1. 10
  2. 20
  3. 30
  4. 40

值可以完全不同,并且彼此不相关。 例如,索引为10的类与索引为20的类没有关联。

例如,

   List<ClassA> unorderedCollection=GetCollection(); 
    /* has values:
      {
         B,
         20,
         uid             
      },
      {
         B,
         20,
         uid
      },
      {
         A,
         10,
         uid             
      },
      {
         A,
         10,
       uid             
      },
      { 
         C,
         30,
         uid             
      },
      {
         D,
         40,
         uid             
      },

    /*

并且订购集合应该是:

   /*
     {
         A,
         10,
         uid             
      },
      {
         A,
         10,
         uid             
      },
      {
         B,
         20,
         uid             
      },
      {
         B,
         20,
         uid             
      },
      {
         C,
         30,
         uid             
      },
      {
         D,
         40,
         uid             
      },
  */

我尝试使用OrderBy,但未能达到预期的效果。

我该怎么做?

4 个答案:

答案 0 :(得分:2)

您可以尝试使用OrderByThenBy

  在ThenBy之后使用

OrderBy方法对另一个字段上的集合进行升序排序。

 unorderedCollection.OrderBy(x => x.ClassName).ThenBy(y => y.ClassId);

答案 1 :(得分:1)

这是有效的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
       static void Main(string[] args)
        {

           List<ClassA> unorderedCollection= new List<ClassA>() {
               new ClassA() { ClassName = "B",  ClassId = 20, UID = new Guid(new byte[] {0,1,2,3,4,5,6,7,8,9,0xA,0xB,0xC,0xD,0xE,0xF})},
               new ClassA() { ClassName =  "B", ClassId = 20, UID = new Guid(new byte[] {0,1,2,3,4,5,6,7,8,9,0xA,0xB,0xC,0xD,0xE,0xF})},
               new ClassA() { ClassName =  "A", ClassId = 10, UID = new Guid(new byte[] {0,1,2,3,4,5,6,7,8,9,0xA,0xB,0xC,0xD,0xE,0xF})},
               new ClassA() { ClassName =  "A", ClassId = 10, UID = new Guid(new byte[] {0,1,2,3,4,5,6,7,8,9,0xA,0xB,0xC,0xD,0xE,0xF})},
               new ClassA() { ClassName =  "C", ClassId = 30, UID = new Guid(new byte[] {0,1,2,3,4,5,6,7,8,9,0xA,0xB,0xC,0xD,0xE,0xF})},
               new ClassA() { ClassName =  "D", ClassId = 40, UID = new Guid(new byte[] {0,1,2,3,4,5,6,7,8,9,0xA,0xB,0xC,0xD,0xE,0xF})},
           };

           List<ClassA> results = unorderedCollection.OrderBy(x => x.ClassName).ThenBy(x => x.ClassId).ToList();


        }


    }
    public class ClassA
    {
        public String ClassName {get;set;}
        public int ClassId {get;set;}
        public Guid UID { get; set;  }
    }

}

答案 2 :(得分:1)

如果要以非字母顺序按ClassName排序,可以使用Comparer

假设我们要按类名对ClassA对象按B,C,A,D的顺序进行排序,我们可以使用以下

private class ClassAComparer : Comparer<ClassA>
{
    private IDictionary<string, int> _lookup = new Dictionary<string, int>
    {
        {"B", 1},
        {"C", 2},
        {"A", 3},
        {"D", 4},
    };

    public override int Compare(ClassA x, ClassA y)
    {
        return _lookup[x.ClassName].CompareTo(_lookup[y.ClassName]);
    }
}

我们使用哪个

//...

var ordered = unordered.OrderBy(n => n, new ClassAComparer());

//…

答案 3 :(得分:0)

我做到了! 这是答案:

    _orderClasses : {A:0, B:1,C:2,D:3}  //0,1,2- is an target order

       var indexes = new Dictionary<string, int>();

        for (int i = 0; i < _orderClasses.Count; i++)
        {                            
            indexes.Add(_orderClasses[i].Name, i);
        }

        var results = _unOrderedResults.OrderBy(x => indexes[x.ClassName]).ToArray();