开发者

LINQ-ing with an ObservableCollection

I am new to using LINQ. I am trying to use it in Silverlight as I'm trying to do a DISTINCT query. My Silverlight application pings a WCF service which returns an ObservableCollection of a custom type. I am trying to get to a DISTINCT record set based on several properties of my custom type. I know the first开发者_开发问答 step is to get my record set, so I'm trying the following

var filteredItems = (from entity in e.Result
                    select new FilteredItem
                    {
                      Property1 = entity.Property1,
                      Property2 = entity.Property2,
                      Property3 = entity.Property3
                    }).Distinct();

Unfortunately, this doesn't work. Intellisense gives me an error that says "Could not find an implementation of the query pattern for source type MyServiceProxy.MyCustomType. Select not found..." How can I use an ObservableCollection with LINQ, or get this distinct set like I'm showing?

Thank you!


ObservableCollection<T> implements IEnumerable<T>, so you should be able to do that if you're using System.Linq. All the standard LINQ operators reside in that namespace. If that doesn't work, then make sure you're referencing System.Core.dll, because that's the assembly that contains those same implementations.


Getting a distinct list with LINQ is as simple as using the Distinct() method. You have to use grouping. Take a look at this:

LINQ to SQL grouping multiple columns with a distinct row


You need my ObservableComputations library maybe. Using this library you can code like this:

var filteredItems = e.Result.Selecting(entity => 
                    new FilteredItem
                    {
                      Property1 = entity.Property1,
                      Property2 = entity.Property2,
                      Property3 = entity.Property3
                    }).Distinct();

In code above I assumed e.Result is ObservableCollection. filteredItems is ObservableCollection and reflects all the changes in the e.Result collection and properties mentioned in the code above. Ensure that all properties mentioned in the code above notify of changes through the INotifyPropertyChanged interface.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜