有没有办法从 ObservableCollection 获取范围?

发布于 2024-09-30 18:41:12 字数 104 浏览 8 评论 0原文

我想从 ObservableCollection 中获取一个范围,以便循环遍历它并更改这些项目的属性。是否有一种简单的内置方法可以使用 ObservableCollection 类来执行此操作?

I would like to get a range from an ObservableCollection for the purpose of looping through it and changing a property on those items. Is there an easy built-in way to do this with the ObservableCollection class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深居我梦 2024-10-07 18:41:12

您可以使用SkipTake

System.Collections.ObjectModel.ObservableCollection<int> coll = 
    new System.Collections.ObjectModel.ObservableCollection<int>()
        { 1, 2, 3, 4, 5 };
foreach (var i in coll.Skip(2).Take(2))
{
    Console.WriteLine(i);
}

You can use Skip and Take.

System.Collections.ObjectModel.ObservableCollection<int> coll = 
    new System.Collections.ObjectModel.ObservableCollection<int>()
        { 1, 2, 3, 4, 5 };
foreach (var i in coll.Skip(2).Take(2))
{
    Console.WriteLine(i);
}
烟花易冷人易散 2024-10-07 18:41:12

对于循环,ObservableCollection派生来自 Collection,并实现 IList。这允许您按索引循环:

for (int i=5;i<10;++i)
{
     DoSomething(observableCollection[i]);
}

如果您想通过 LINQ 对集合进行查询,您有几个选择。您可以使用 Skip()Take(),或构建新范围并按索引访问:

var range = Enumerable.Range(5, 5);
var results = range.Select(i => DoMappingOperation(observableCollection[i]));

或者:

var results = observableCollection.Skip(5).Take(5).Select(c => DoMappingOperation(c));

For looping, ObservableCollection<T> derives from Collection<T>, and implements IList<T>. This lets you loop by index:

for (int i=5;i<10;++i)
{
     DoSomething(observableCollection[i]);
}

If you want to do queries on the collection via LINQ, you have a couple of options. You can use Skip() and Take(), or build a new range and access by index:

var range = Enumerable.Range(5, 5);
var results = range.Select(i => DoMappingOperation(observableCollection[i]));

Or:

var results = observableCollection.Skip(5).Take(5).Select(c => DoMappingOperation(c));
奢华的一滴泪 2024-10-07 18:41:12

ObservableCollection 实现 Collection ,它实现 IEnumerable 所以你应该能够做到(如果你正在寻找一个范围)匹配给定条件):

foreach(var item in observerableCollection.Where(i => i.prop == someVal))
{
    item.PropertyToChange = newValue;
}

或任意范围(在本例中需要项目 10 - 40):

foreach(var item in observableCollection.Skip(10).Take(30))
{
    item.PropertyToChange = newValue;
}

ObservableCollection<T> implements Colletion<T> which implements IEnumerable so you should be able to do (if you're looking for a range that matches a given criteria):

foreach(var item in observerableCollection.Where(i => i.prop == someVal))
{
    item.PropertyToChange = newValue;
}

Or an arbitrary range (in this case it takes items 10 - 40):

foreach(var item in observableCollection.Skip(10).Take(30))
{
    item.PropertyToChange = newValue;
}
云归处 2024-10-07 18:41:12
foreach(var item in MyObservableProperty.Skip(10).Take(20))
{
  item.Value = "Second ten";
}

跳过Take 是用于对集合进行分页的 linq 扩展方法。

foreach(var item in MyObservableProperty.Skip(10).Take(20))
{
  item.Value = "Second ten";
}

Skip and Take are linq extension methods used for paging a collection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文