C#Linq使用参考列表订购列表 [英] C# Linq Order a list with a reference list
本文介绍了C#Linq使用参考列表订购列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个列表,我需要对另一个列表进行排序.
I have two lists and I need to sort a list with another list.
var orderList = new List<long>() {4, 55, 34};
var itemList = new List<Branch>() { {Id=55, Name="X"}, {Id=34, Name="Y"}, {Id=4, Name="Z"} };
如何根据第一个orderList
排序第二个列表,即itemList
?
How can I order second list namely itemList
according to first one orderList
?
有什么linq捷径吗?
Is there any linq short way?
更新:
orderList
始终将所有预期的Id
分支包含在itemList
中.
orderList
will always have all expected Id
branch contained itemList
.
推荐答案
您可以使用LINQ
并按其在其他数组中的索引对项目进行排序:
You could use LINQ
and order the items by their index in other array:
var orderedItemList = itemList.OrderBy(x => orderList.IndexOf(x.Id)).ToList();
这篇关于C#Linq使用参考列表订购列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文