我有一个列表(myList),其中包含按序列排序的表的名称。MyObject 有 2 个属性 Table (string) 和 Sequence (int)
List<MyObject>
例子:
List<MyObject>[0] = MyObject.Sequence = 0, MyObject.Table = "Cat"
List<MyObject>[1] = MyObject.Sequence = 1, MyObject.Table = "Dog"
List<MyObject>[2] = MyObject.Sequence = 2, MyObject.Table = "Bird"
List<MyObject>[3] = MyObject.Sequence = 3, MyObject.Table = "Fish"
然后我得到了一个带有结构键的字典,其中有表和实体对象作为值,所以这个字典已经填充了 10 个像这样的对象值
dict[0] = key (ID = 1, Table = "Cat" ), EntityObject = Cat
dict[1] = key (ID = 2, Table = "Dog"), EntityObject = Dog
dict[2] = key (ID = 3, Table = "Bird"), EntityObject = Bird
dict[3] = key (ID = 4, Table = "Fish"), EntityObject = Fish
dict[4] = key (ID = 5, Table = "Cat" ), EntityObject = Cat
dict[5] = key (ID = 6, Table = "Dog"), EntityObject = Dog
dict[6] = key (ID = 7, Table = "Cat" ), EntityObject = Cat
dict[7] = key (ID = 8, Table = "Dog"), EntityObject = Dog
dict[8] = key (ID = 9, Table = "Cat" ), EntityObject = Cat
dict[9] = key (ID = 10, Table = "Fish"), EntityObject = Fish
所以我想根据包含表序列的列表来排序我的字典并重建字典以使其像这样
dict[0] = key (ID = 1, Table = "Cat"), EntityObject = Cat
dict[1] = key (ID = 5, Table = "Cat"), EntityObject = Cat
dict[2] = key (ID = 7, Table = "Cat"), EntityObject = Cat
dict[3] = key (ID = 9, Table = "Cat"), EntityObject = Cat
dict[4] = key (ID = 2, Table = "Dog"), EntityObject = Dog
dict[5] = key (ID = 6, Table = "Dog"), EntityObject = Dog
dict[6] = key (ID = 8, Table = "Dog"), EntityObject = Dog
dict[7] = key (ID = 3, Table = "Bird"), EntityObject = Bird
dict[8] = key (ID = 4, Table = "Fish"), EntityObject = Fish
dict[9] = key (ID = 10, Table = "Fish"), EntityObject = Fish
我想用 foreach避免循环,然后重新创建一个新字典,然后复制或克隆到现有字典中。所以,我试了一下这个飞行,但我总是得到相同的未排序列表。也许我错过了 Sort 或 OrderBy 方法,但有人可以帮助解决这个问题。建议?
Dictionary<MyStruct, EntityObject> dict = this.GetDictionary()
.ToList()
.FindAll(x => x.Key.Table == myList.Find(y => y.Table == x.Key.Table).Table)
.ToDictionary(a => a.Key, b => b.Value);
笔记:
伙计们,我已经知道无法对字典进行排序,但是如果您看到我的方法,我正在重新创建一个新字典,试图对内存中的元素进行排序并将其分配给“dict”。
- -编辑 - -
我最近做了这个修改,但仍然没有。请帮忙。
Dictionary<MyStruct, EntityObject>
dict = this.GetDictionary()
.ToList()
.OrderBy(x => x.Key.Table == myList
.Find(z => z.Table == x.Key.Table).Table)
.ToDictionary(x => x.Key, y => y.Value);