简体   繁体   English

使用 ObservableCollection 的 LINQ

[英]LINQ-ing with an ObservableCollection

I am new to using LINQ.我是使用 LINQ 的新手。 I am trying to use it in Silverlight as I'm trying to do a DISTINCT query.我正在尝试在 Silverlight 中使用它,因为我正在尝试执行 DISTINCT 查询。 My Silverlight application pings a WCF service which returns an ObservableCollection of a custom type.我的 Silverlight 应用程序 ping 一个 WCF 服务,该服务返回一个自定义类型的 ObservableCollection。 I am trying to get to a DISTINCT record set based on several properties of my custom type.我试图根据我的自定义类型的几个属性获取 DISTINCT 记录集。 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? Intellisense 给了我一个错误,提示“找不到源类型 MyServiceProxy.MyCustomType 的查询模式的实现。选择未找到... ”我如何在 LINQ 中使用 ObservableCollection,或者像我展示的那样获取这个不同的集合?

Thank you!谢谢!

ObservableCollection<T> implements IEnumerable<T> , so you should be able to do that if you're using System.Linq . ObservableCollection<T>实现IEnumerable<T> ,因此如果您using System.Linq ,您应该能够做到这一点。 All the standard LINQ operators reside in that namespace.所有标准 LINQ 运算符都驻留在该命名空间中。 If that doesn't work, then make sure you're referencing System.Core.dll, because that's the assembly that contains those same implementations.如果这不起作用,那么请确保您正在引用 System.Core.dll,因为这是包含这些相同实现的程序集。

Getting a distinct list with LINQ is as simple as using the Distinct() method.使用 LINQ 获取不同列表就像使用 Distinct() 方法一样简单。 You have to use grouping.你必须使用分组。 Take a look at this:看看这个:

LINQ to SQL grouping multiple columns with a distinct row LINQ to SQL 使用不同的行对多列进行分组

You need my ObservableComputations library maybe.你可能需要我的ObservableComputations库。 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 .在上面的代码中,我假设 e.Result 是ObservableCollection filteredItems is ObservableCollection and reflects all the changes in the e.Result collection and properties mentioned in the code above. FilteredItems 是ObservableCollection并反映了上面代码中提到的 e.Result 集合和属性中的所有更改。 Ensure that all properties mentioned in the code above notify of changes through the INotifyPropertyChanged interface.确保上面代码中提到的所有属性都通过INotifyPropertyChanged接口通知更改。

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

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