我有一个项目列表(实际上是IEnumerable)。每个项目都有一个值列表。例如:
Item1.Value[0] = "Health & Safety"
Item1.Value[1] = "Economic"
Item1.Value[2] = "Environment"
Item2.Value[0] = "Reputation"
Item2.Value[1] = "Environment"
Item2.Value[2] = "Regulatory"
...
如何使用linq订购值列表?我知道我可以使用以下内容订购商品清单:
Items.Orderby(x => x.something)
...但是如何到达每个项目中的值列表?
答案 0 :(得分:0)
你可以试试这个
Items.ForEach(i=> x.something = x.something.OrderBy(o=> o.field));
答案 1 :(得分:0)
编辑:根据OP的评论,Value
是Dictionary<string, object>
。您无法对Dictionary
进行排序,因为它们在设计上是无序的。
考虑使用SortedDictionary<TKey, TValue>
,并实施IComparer<TKey>
以满足您的排序需求:
示例:
Dictionary<string, object> values = new Dictionary<string, object>
{
{ "b", 1 }, { "a", 2 }, { "c", 3 }
};
// { { "a", 2 }, { "b", 1 }, { "c", 3 } }
SortedDictionary<string, object> keyAscending =
new SortedDictionary<string, object>(values);
public class ReverseStringComparer : IComparer<string>
{
int IComparer<string>.Compare(string x, string y)
{
return y.CompareTo(x);
}
}
// { { "c", 3 }, { "b", 1 }, { "a", 2 } }
SortedDictionary<string, object> keyDescending =
new SortedDictionary<string, object>(values, new ReverseStringComparer());