![](/img/trans.png)
[英]C#: is there a way to set a property of objects in a collection using lambda expression?
[英]Lambda expression for Order List of collection of objects based on a property of the object in C#
我的要求
基於一個值,我需要訂購一個列表,該列表具有對象列表的集合。
對對象列表進行排序的比較是檢查對象中具有預定義值的屬性。
例
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
List2
所有sig
對象的鍵值為505
all `sig` objects inside `List3` will have key value as 249.
對於比較值,我有一個字符串數組,例如keys[] = {505,501,249}
預期結果
列表ListAll
的結果順序應該是這樣的,使得列表應該按照屬性key
的值進行排序,就像數組keys[]
中存在的順序一樣,因此該順序應如下所示:
List2
List1
List3
我的問題
如何使用數組中值的順序對對象列表進行排序。
可以按照以下步驟完成:
listAll = listAll.OrderBy(e => Array.IndexOf(keys, e.FirstOrDefault()?.key))
.ToList();
我問您可以先用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);
}
}
如果您知道每個列表中至少包含1個元素,則可以執行以下操作:
listAll = listAll.OrderBy(e => e.First().key).ToList();
如果您不知道,可以使用FirstOrDefault
並應用一些與您的空列表處理決策相對應的邏輯。
您可以簡單地按以下方式使用order來根據列表的鍵值對列表進行排序。 (您不需要帶有其鍵的另一個數組)
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; }
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.