绑定linq表达式 [英] bind linq expression

查看:116
本文介绍了绑定linq表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的wpf应用程序中有一个ListView和ObservableCollection。
我想将linq表达式绑定到ListView:

  lv_Results.DataContext = store.Where(x => x .PR> 5).ToList(); 
tempStore = new M1Store()
{
SiteName = siteName,
Date = date,
url = IndexedPage,
TCY = YandexTCY.GetYandexTCY(IndexedPage ),
PR = Pagerank.GetPR(IndexedPage)
};
store.Add(tempStore);

但是,当我添加新的元素来存储集合时,lv_Results不会更新。
如何更新ListView?

解决方案

您的问题是您希望连续评估Where条件在所有添加到商店集合的项目。内置的LINQWhere操作符并不是为了这样做而设计的。相反,当它被ListView枚举时,它会扫描您的集合一次,然后从此忽略它。



查看连续LINQ 。它被设计为完全符合您的要求,并且几乎可以用作标准LINQ查询的替代品。



已构建的限制 - LINQ实现



内置的LINQ扩展方法有一个根本的限制,因为它们生成的集合不支持INotifyPropertyChanged。因此,无论基础数据发生多少变化,客户端将永远不会收到通知,指出收集已经更改,因此永远不会刷新其数据显示。



用户jrista点在评论中,如果重新枚举,内置的LINQ方法实际上会生成最新的数据。虽然如此,但这没有实际的效果。没有WinForms或WPF控件包含定期重新枚举其数据源的代码。不这样做的原因当然是显而易见的:这将是非常浪费的。如果您每秒重新枚举10次,需要10ms才能重新枚举和扫描更改,您将只占用一个控件的CPU的10%!


I have a ListView and ObservableCollection in my wpf application. I want to bind linq expression to ListView:

lv_Results.DataContext = store.Where(x => x.PR > 5).ToList();
tempStore = new M1Store()
                            {
                                SiteName = siteName,
                                Date = date,
                                url = IndexedPage,
                                TCY = YandexTCY.GetYandexTCY(IndexedPage),
                                PR = Pagerank.GetPR(IndexedPage)
                            };
            store.Add(tempStore);

But when i add new elements to store collection, lv_Results doesnt updates. How can i update ListView?

解决方案

Your problem is that you want the "Where" condition to be evaluated continuously on all items added to the "store" collection. The built-in LINQ "Where" operator is not designed to do that. Rather, when it is enumerated by the ListView it scans your collection exactly once then ignores it from then on.

Check out Continuous LINQ. It is designed to do exactly what you are looking for, and can be used almost as a drop-in replacement for standard LINQ queries.

Limitations of the built-in LINQ implementation

The built-in LINQ extension methods have a fundamental limitation in that the collections they produce don't support INotifyPropertyChanged. So no matter how much the underlying data changes, the client will never receive notification that the collection has changed and hence will never refresh its display of the data.

User jrista points out in the comments that the built-in LINQ methods do actually produce up-to-date data if re-enumerated. While true, this has no practical effect. No WinForms or WPF control contains code to periodically re-enumerate its data source. The reasons for not doing so are, of course, obvious: It would be incredibly wasteful. If you re-enumerate 10 times per second and it takes 10ms to re-enumerate and scan for changes you will use up 10% of your CPU for just one control!

这篇关于绑定linq表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆