如何创建“现场”集合是 2 个(或更多)其他集合的串联/并集/其他集合吗?

发布于 2024-10-31 12:12:07 字数 1083 浏览 0 评论 0原文

注意:我当然可以通过创建一个实现 INotifyCollectionChanged 的​​类来暴力破解这个问题,但感觉就像已经解决了一些问题,而我只是还没有找到正确的位置。 >

在我的视图模型中的几个地方,我的集合实际上是我的域上下文中的一些特定实体(我们将其称为 SomeEntitySet),并添加了一些“特殊”实例(在前面,在结束,有时两者都有)。目前,我将其实现为一个简单的 ObservableCollection(我们将其称为 SupplementedEntitySet),每当我更新特定实体集时,我都会让回调重新填充此 SupplementedEntitySet。域上下文的实体设置是只读的,因此我无法在那里添加“特殊”实例(而且我真的不想这样做,因为我不希望它们写回数据库)。

对于这种情况,我们假设 1) 我不想修改 RIA 查询以包含这些“特殊”实体 2) 我不想在使用站点包含绑定转换器来添加它们等。

虽然您可能不会这样做不要使用这种特定的机制来执行此操作,作为示例场景,假设您有一个从 RIA 调用返回的类别列表,但您希望将组合框绑定到的集合具有特殊的“所有项目”类别,如下所示好吧(再说一遍,不想通过转换器等添加它)。

在概念层面上,我通常会查看类似 Rx 的东西,但至少目前,我不知道如何使用它,这样我仍然有一个可用于绑定为 ItemsSource 的“拉”集合对于某些 ItemsControl(网格、组合框、列表框等)

当然,这是一个更通用问题的特定情况 - 如何获得“实时”linq 查询输出 - 您可能需要作为源子集的“实时”集合,或者投影,或某种组合,或其他什么。就像我们有 IEnumerable、IQueryable 和 IObservable 的 LINQ 提供程序一样,感觉我们可以/应该有一个 IObservableCollection 之类的提供程序(其中 IObservableCollection :ICollection、INotifyCollectionChanged 或类似的东西),

更通用的框架/提供程序/任何看起来相当复杂的东西(特别是以高性能的方式实现它),所以至少目前,我只是在寻找源集合的特定场景(在本例中是 ObservableCollection,即使 DomainContext 仅将它们公开为 IEnumerable

)这样想有错吗?

NOTE: I can certainly brute-force this myself by making a class that implements INotifyCollectionChanged, but it feels like something that's already been solved, and I just haven't looked in the right place(s) yet.

In a few places in my viewmodel, I have collections that are effectively some particular entity set off of my domain context (we'll call it SomeEntitySet) with some small handful of 'special' instances added to it (at the front, at the end, sometimes both). Currently I implement this as a simple ObservableCollection (we'll call it SupplementedEntitySet) and whenever I update the particular entity set, I have the callback also re-populate this SupplementedEntitySet. The entity set off of the domain context is readonly, so I can't add the 'special' instances there (and I really wouldn't want to anyway, as I don't want them written back to the database).

For this scenario, let's assume 1) I don't want to modify the RIA query to include these 'special' entities 2) I don't want to include binding converters at use sites to add them, etc.

While you likely wouldn't use this particular mechanism to do so, as an example scenario, let's say you have a list of categories coming back from the RIA call, but you want the collection to which you bind your combobox to have a special 'All items' category as well (and again, don't want to add it via converter or the like).

At a conceptual level, I'd normally look at something like Rx, but at least at the moment, I don't see how I would use that such that I'd still have a 'pull' collection useful for binding as the ItemsSource to some ItemsControl (grid, combobox, listbox, whatever)

Certainly this is a specific case of a more generic problem - how to have 'live' linq query outputs - you might want 'live' collections that are subsets of the source, or a projection, or some combination, or whatever. Much like we have LINQ providers for IEnumerable, IQueryable, and IObservable, it feels like we could/should have a provider for something like IObservableCollection (where IObservableCollection : ICollection, INotifyCollectionChanged or something similar)

That more generic framework/provider/whatever seems pretty complicated (especially implementing it in a performant way), so at least at the moment, I'm just looking for the particular scenario of a source collection (in this case, a ObservableCollection, even though the DomainContext only exposes them as IEnumerable)

Am I thinking about this wrong?

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

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

发布评论

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

评论(1

尛丟丟 2024-11-07 12:12:07

您在问题中提到了 Rx,但您不确定如何最终得到用于绑定的“拉”集合。这很容易。您只需要“推”“拉”集合即可。

从这样的主题开始:

var updatedItems = new Subject<IEnumerable<string>>();

主题是 IObservable 和 IObservable 。 IObserver 同时。因此,每次从 RIA 服务获取项目时,您只需调用该主题的 OnNext 即可。

// first result from RIA service call
updatedItems.OnNext(new [] { "A", "B", });

// sometime later another result
updatedItems.OnNext(new [] { "X", "Y", });

现在您可以像这样更新您的 ObservableCollection

var itemsObservableList = new ObservableCollection<string>();

var extras = new [] { "<all items>" };

var supplementedItems = 
    from items in updatedItems
    select extras.Concat(items);

supplementedItems.Subscribe(items =>
{
    itemsObservableList.Clear();
    items.Run(itemsObservableList.Add);
});

这样您只需更新主题一次,所有关联的视图模型就会自动更新。 :-)

You mentioned Rx in your question, but you weren't sure how to end up with a "pull" collection for binding. It's easy. You just need to "push" your "pull" collections.

Start with a subject like this:

var updatedItems = new Subject<IEnumerable<string>>();

Subjects are IObservable & IObserver at the same time. So each time you fetch the items from RIA services you just call OnNext on the subject.

// first result from RIA service call
updatedItems.OnNext(new [] { "A", "B", });

// sometime later another result
updatedItems.OnNext(new [] { "X", "Y", });

Now you can update your ObservableCollection like this:

var itemsObservableList = new ObservableCollection<string>();

var extras = new [] { "<all items>" };

var supplementedItems = 
    from items in updatedItems
    select extras.Concat(items);

supplementedItems.Subscribe(items =>
{
    itemsObservableList.Clear();
    items.Run(itemsObservableList.Add);
});

This way you just need to update the subject once and all of your associated view models get updated automagically. :-)

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