C# 将 LINQ 结果转换为 ObservableCollection
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1465285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cast LINQ result to ObservableCollection
提问by stiank81
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. The result is an;
我运行一个 LINQ 查询。结果是;
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). I notice LINQ has got a few To..() functions, but it doesn't seem to help me for this cast..?
我怎么做这个演员?(无需运行 IEnumerable 并将元素复制到 ObservableCollection)。我注意到 LINQ 有一些 To..() 函数,但它似乎对这个演员没有帮助..?
采纳答案by Jon Skeet
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.
这将进行所需的复制。没有办法观察实时查询的变化——尽管 an 的想法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);
}
回答by bruno conde
You can use an ObservableCollection
constructorfor this:
您可以为此使用ObservableCollection
构造函数:
ObservableCollection<MyClass> obsCol =
new ObservableCollection<MyClass>(myIEnumerable);
回答by Dave Markle
var linqResults = foos.Where(f => f.Name == "Widget");
var observable = new ObservableCollection<Foo>(linqResults);
回答by Heiko Hatzfeld
IEnumerable is only the interface.
IEnumerable 只是接口。
You would need to copy the content from the IEnumerable into the ObservableCollection. You can do this by passing your IEnumerable into the constructor of the ObersvableCollection when you create a new one
您需要将 IEnumerable 中的内容复制到 ObservableCollection 中。您可以通过在创建新集合时将 IEnumerable 传递给 ObersvableCollection 的构造函数来实现此目的
回答by Jerome Haltom
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. But the code base is not hard to extend.
它缺少一些 Linq 运算符。但是代码库并不难扩展。
回答by Igor Buchelnikov
You need my ObservableComputationslibrary maybe. That is .NET API for computations over INotifyPropertyChanged and INotifyColectionChanged (ObservableCollection) objects. Results of the computations are INotifyPropertyChanged and INotifyColectionChanged (ObservableCollection) objects.
你可能需要我的ObservableComputations库。这是用于计算 INotifyPropertyChanged 和 INotifyCollectionChanged (ObservableCollection) 对象的 .NET API。计算结果是 INotifyPropertyChanged 和 INotifyCollectionChanged (ObservableCollection) 对象。