获得“下一个”的最简单方法序列中的元素?
我有一个 ICollection
。
public class SomeClass
{
public string Text { get; set; }
public bool IsPreferred { get; set; }
}
那里的商品已经被预订了,所以“下一个”确实有某种意义。
在我的场景中,序列的内容如下所示:
[0] - “a”,假
[1] - “b”,正确
[2] - “c”,假
I'我试图获取 IsPreferred == true
之后的“下一个”元素。所以在上面,我想获取元素 2,并且我想清除其他 IsPreferred
值。
所以我想以此结束:
[0] - “a”,假
[1] - “b”,假
[2] - “c”,true
基本上是随机播放首选项目下降。
最好的方法是什么?我唯一能想到的就是创建一个新数组,将它们一一添加,跟踪首选数组的索引,然后去抓取上述索引+1处的元素。
还有更好的想法吗?
I've got an ICollection<SomeClass>
.
public class SomeClass
{
public string Text { get; set; }
public bool IsPreferred { get; set; }
}
The items in there have been pre-ordered, so "next" does mean something.
In my scenario, the contents of the sequence looks like this:
[0] - "a", false
[1] - "b", true
[2] - "c", false
I'm trying to get the "next" element after the one that IsPreferred == true
. So in the above, i want to get element 2, and i want to clear the other IsPreferred
value.
So i want to end up with this:
[0] - "a", false
[1] - "b", false
[2] - "c", true
Basically shuffling the preferred item down.
What's the best way to do it? The only thing i can think of is to create a new array, add them one by one, keep track of the index of the one that is preferred, then go grab the element at the aforementioned index +1.
Any better ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我将使用枚举器来迭代集合 - 这就是 foreach 在幕后所做的事情:
这确实假设您想在使用 IsPreferred 找到第一个项目后停止,并且如果这是最后一个项目,则仍然清除 IsPreferred。
编辑:修复了在单个项目的集合上 IsPreferred 始终设置为 false 的边缘情况
I would use an enumerator to iterate over the collection - this is what foreach does behind the scenes:
This does assume that you want to stop after finding the first item with IsPreferred, and if this is the last item, still clear IsPreferred.
Edit: Fixed edge case where IsPreferred is always set to false on a collection of a single item
由于
ICollection
不给你一个索引器我会选择更直接的解决方案而不是依赖 LINQ。假设您希望 (1) 一旦满足条件就停止,并且 (2) 仅在下一项存在时更改值,可以按如下方式实现:
Since
ICollection<T>
doesn't give you an indexer I would opt for a more direct solution rather than rely on LINQ.Assuming you want to (1) stop as soon as the condition is met, and (2) only change the value if the next item exists, this can be achieved as follows:
我只能想到一种用 LINQ 来完成此操作的混乱方法:
这当然排除了错误检查,并且效率不高。
另一种可能性(使用 LINQ)是使用 Ahmad Mageed 的解决方案(如下面的评论中所建议的):
I can only think of a messy way doing this with LINQ:
This of course excludes error checking and is not very efficient.
Another possibility (using LINQ) would be to use Ahmad Mageed's solution (as suggested in the comments below):
几个想法
Couple of ideas
既然你的集合是有序的,它可以是 IList 而不是 ICollection 吗?
然后,您可以创建一个扩展方法来为您提供某个谓词适用的值的索引:
假设您期望只有一个元素匹配,那么听起来其余的元素将如下所示:
Since your collection is ordered, can it be an IList instead of an ICollection?
Then you could create an extension method to give you the indexes of the values where some predicate applies:
Assuming you expect only one element to ever match, it sounds like the rest would just look like this: