gpt4 book ai didi

c# - 使用 LINQ 自定义排序

转载 作者:行者123 更新时间:2023-11-30 19:08:10 24 4
gpt4 key购买 nike

看来我遗漏了一些微不足道的东西。

无论如何,这里是:

var order = new[]{1,3,2};
var foos = new[]{new Foo{Id=1}, new Foo{Id=2}, new Foo{Id=3}};

如何使用 Linq 按顺序数组对 foo 进行排序?

期望的结果:

foos == new[]{new Foo{Id=1}, new Foo{Id=3}, new Foo{Id=2}};

编辑:
订单包含 Foo id。抱歉,我没有提到这一点。有时,正确地提出问题比回答问题更难。 :)

最佳答案

这就是你想要做的吗?

foos.OrderBy(f => order[f.Id-1]);

如果你现在 foreach 输出,打印 ID,你会得到:1,3,2

关于c# - 使用 LINQ 自定义排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1443753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com