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!