LINQ orderby int 数组索引值
使用 LINQ 我想按传入的 int 数组索引进行排序。
所以在下面的代码中 attribueIds 是我的 int 数组。我将该数组中的整数用于 where 子句,但我希望结果按照它们在数组中的顺序排列。
public List BuildTable(int[] attributeIds) { using (var dc = new MyDC()) { var ordering = attributeIds.ToList(); var query = from att in dc.DC.Ecs_TblAttributes where attributeIds.Contains(att.ID) orderby(ordering.IndexOf(att.ID)) select new Common.Models.Attribute { AttributeId = att.ID, DisplayName = att.DisplayName, AttributeName = att.Name }; return query.ToList(); } }
Using LINQ I would like to sort by the passed in int arrays index.
So in the code below attribueIds is my int array. I'm using the integers in that array for the where clause but I would like the results in the order that they were in while in the array.
public List BuildTable(int[] attributeIds) { using (var dc = new MyDC()) { var ordering = attributeIds.ToList(); var query = from att in dc.DC.Ecs_TblAttributes where attributeIds.Contains(att.ID) orderby(ordering.IndexOf(att.ID)) select new Common.Models.Attribute { AttributeId = att.ID, DisplayName = att.DisplayName, AttributeName = att.Name }; return query.ToList(); } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议从 attributeIDs 数组中进行选择。这将确保您的物品能够正确排序,而无需排序。
代码应该是这样的:
I would recommend selecting from the attributeIDs array instead. This will ensure that your items will be correctly ordered without requiring a sort.
The code should go something like this:
你为什么不加入:
Why don't you join: