基于C#中对象属性的对象集合的Order List的Lambda表达式

时间:2018-06-14 09:21:06

标签: c# linq lambda

我的要求

基于一个值,我需要订购一个列表,该列表包含一个对象列表的集合。

要对对象列表进行排序的比较是检查对象中具有预定义值的属性。

实施例

ListAll =  {List1<sig>,List2<sig>,List3<sig>}
  • 假设对象sig具有属性key, name, value

  • 每个列表 - List1<sig>,List2<sig>,List3<sig> - 将具有相同的属性值key

        all `sig` objects inside `List1` will have key value as 501 
    

    sig内的所有List2个对象的密钥值为505

         all `sig` objects inside `List3` will have key value as 249.
    

对于比较值,我有一个字符串数组说keys[] = {505,501,249}

预期结果

列表ListAll中的结果顺序应该是这样的,列表应该按属性key的值排序,作为数组keys[]中存在的顺序 所以订单应该是这样的:

List2
List1
List3 

我的问题

如何使用数组内的值顺序来排序对象列表。

4 个答案:

答案 0 :(得分:2)

可以按照以下方式完成:

listAll = listAll.OrderBy(e => Array.IndexOf(keys, e.FirstOrDefault()?.key))
                 .ToList();

dotnetfiddle

答案 1 :(得分:0)

我认为你可以先用Linq的SelectMany将列表展平,然后再订购并重新组合。

像这样的东西

public class Item
{
  public int Key {get;set;}
  public string Name {get;set;}
}

public class Program
{
  public static void Main()
  {
    List<List<Item>> AllList = new List<List<Item>>();

    AllList.Add(new List<Item>() { new Item(){ Key = 503, Name = "A" }, new Item(){ Key = 503, Name = "B" }, new Item(){ Key = 503, Name = "C" }});
    AllList.Add(new List<Item>() { new Item(){ Key = 500, Name = "A" }, new Item(){ Key = 500, Name = "B" }, new Item(){ Key = 500, Name = "C" }});
    AllList.Add(new List<Item>() { new Item(){ Key = 204, Name = "A" }, new Item(){ Key = 204, Name = "B" }, new Item(){ Key = 204, Name = "C" }});

    var result = AllList.SelectMany(item => item).OrderBy(item => item.Key).GroupBy(item => item.Key);

    foreach(var item in result)
      foreach(var innerItem in item)
        Console.WriteLine(innerItem.Key);
  }
}

答案 2 :(得分:0)

如果您知道每个列表中至少包含1个元素,则可以执行以下操作:

listAll =  listAll.OrderBy(e => e.First().key).ToList();

如果您不知道,可以使用FirstOrDefault并应用与空列表处理决策相对应的逻辑。

答案 3 :(得分:0)

您可以简单地使用下面的顺序,根据键值对列表进行排序。 (你不需要另一个带有键的数组)

var sortedList = allList.OrderBy(lst => lst.FirstOrDefault()?.Key).ToList();

以下是一个完整的工作示例

namespace ConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var lst1 = new List<Sig>()
            {
                new Sig
                {
                    Key=501,
                    Value="value 501"
                },
                new Sig
                {
                    Key=501,
                    Value="another value 501"
                }
            };

            var lst2 = new List<Sig>()
            {
                new Sig
                {
                    Key=505,
                    Value="value 505"
                },
                new Sig
                {
                    Key=505,
                    Value="another value 505"
                }
            };

            var lst3 = new List<Sig>()
            {
                new Sig
                {
                    Key=502,
                    Value="value 502"
                },
                new Sig
                {
                    Key=502,
                    Value="another value 502"
                }
            };

            var allList = new List<List<Sig>>()
            {
                lst1,lst2,lst3
            };

            Console.WriteLine("Original list:");
            PrintList(allList);

            var sortedList = allList.OrderBy(lst => lst.FirstOrDefault()?.Key).ToList();

            Console.WriteLine("Sorted List");
            PrintList(sortedList);

            Console.ReadKey();
        }

        static void PrintList(List<List<Sig>> allList)
        {
            foreach (var lst in allList)
            {
                Console.WriteLine("List 1");
                foreach (var l in lst)
                {
                    Console.WriteLine($"Key:{l.Key} Value:{l.Value}");
                }
            }
        }
    }

    public class Sig
    {
        public int Key { get; set; }
        public string Value { get; set; }
    }
}