简体   繁体   English

将 LINQ 结果转换为 ObservableCollection

[英]Cast LINQ result to ObservableCollection

The fact that it is a LINQ result might perhaps not be relevant for the question, but I'm mentioning it anyway - since this is the context which has resulted in this question.它是 LINQ 结果的事实可能与该问题无关,但无论如何我都会提到它 - 因为这是导致这个问题的上下文。

I run a LINQ query.我运行一个 LINQ 查询。 The result is an;结果是;

IEnumerable<MyClass> 

I want to put the result into an ObservableCollection;我想把结果放到一个 ObservableCollection 中;

ObservableCollection<MyClass> 

How do I do this cast?我怎么做这个演员? (without running through the IEnumerable and copying elements to the ObservableCollection). (无需运行 IEnumerable 并将元素复制到 ObservableCollection)。 I notice LINQ has got a few To..() functions, but it doesn't seem to help me for this cast..?我注意到 LINQ 有一些 To..() 函数,但它似乎对这个演员没有帮助..?

Just use:只需使用:

ObservableCollection<Foo> x = new ObservableCollection<Foo>(enumerable);

That will do the required copying.这将进行所需的复制。 There's no way of observing changes to the live query - although the idea of an ObservableQuery<T> is an interesting (though challenging) one.没有办法观察实时查询的变化——尽管ObservableQuery<T>的想法很有趣(虽然具有挑战性)。

If you want an extension method to do this, it's simple:如果你想要一个扩展方法来做到这一点,这很简单:

public static ObservableCollection<T> ToObservableCollection<T>
    (this IEnumerable<T> source)
{
    if (source == null)
    {
        throw new ArgumentNullException("source");
    }
    return new ObservableCollection<T>(source);
}
var linqResults = foos.Where(f => f.Name == "Widget");

var observable = new ObservableCollection<Foo>(linqResults);

You can use an ObservableCollection constructor for this:您可以为此使用ObservableCollection构造函数

ObservableCollection<MyClass> obsCol = 
        new ObservableCollection<MyClass>(myIEnumerable);

IEnumerable is only the interface. IEnumerable 只是接口。

You would need to copy the content from the IEnumerable into the ObservableCollection.您需要将 IEnumerable 中的内容复制到 ObservableCollection 中。 You can do this by passing your IEnumerable into the constructor of the ObersvableCollection when you create a new one您可以通过在创建新集合时将 IEnumerable 传递给 ObersvableCollection 的构造函数来实现此目的

I wrote this library a few years ago.几年前我写了这个库。

https://github.com/wasabii/OLinq https://github.com/wasabii/OLinq

It doesn't do exactly what you probably want, it does more.它并不完全符合您的要求,它做得更多。 It's a Linq query provider which parses the expression tree, attaches to the referenced collections, and exposes another collection which emits events upon changes.它是一个 Linq 查询提供程序,它解析表达式树,附加到引用的集合,并公开另一个在更改时发出事件的集合。

It's missing a few Linq operators.它缺少一些 Linq 运算符。 But the code base is not hard to extend.但是代码库并不难扩展。

You need my ObservableComputations library maybe.你可能需要我的ObservableComputations库。 That is .NET API for computations over INotifyPropertyChanged and INotifyColectionChanged (ObservableCollection) objects.这是用于计算 INotifyPropertyChanged 和 INotifyCollectionChanged (ObservableCollection) 对象的 .NET API。 Results of the computations are INotifyPropertyChanged and INotifyColectionChanged (ObservableCollection) objects.计算结果是 INotifyPropertyChanged 和 INotifyCollectionChanged (ObservableCollection) 对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM