在一个规范很糟糕的界面中,我们有
int[] userIdList; // ids of all users to display
string[] userNameList; // names of all users to display
限制是 userIdList[i] 总是包含名为 userNameList[i] 的用户 ID。
新的要求是按名称对用户进行排序:我将获取这两个列表,按名称排序,并返回上述限制仍然成立的两个列表。
当然我可以自己实现排序功能,但我认为单行 linq 语句可以做得更好?我起来了
userNameList.Zip(userIdList, (name, id) => new {name, id}).OrderBy(a=>a.name).
但现在我不知道如何再次解压缩列表...
最佳答案
int[] userIdList = zipped.Select(x => x.id).ToArray();
string[] userNameList = zipped.Select(x => x.name).ToArray();
关于c# - 在 LINQ 中解压压缩列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26357448/