可绑定的LINQ与连续的LINQ
可绑定LINQ和连续LINQ之间的主要区别是什么?
可绑定的LINQ:www.codeplex.com/bindablelinq
连续LINQ:www.codeplex.com/clinq
根据提供的反馈,又添加了一个项目:
Obtics:obtics.codeplex.com
解决方案
这两个软件包都试图解决两个问题:缺少CollectionChanged事件和动态结果集。可绑定解决的另一个问题是其他自动事件触发器。
两个软件包都旨在解决的第一个问题是:
Objects returned by a LINQ query do not provide CollectionChanged events.
连续LINQ无需更改即可自动对所有查询执行此操作:
from item in theSource select item ;
将.asBindable添加到查询源对象时,可绑定LINQ会执行此操作:
from item in theSource.AsBindable() select item ;
这两个软件包都旨在解决的第二个问题是:
Result sets returned from a LINQ Query are static.
通常,当我们执行LINQ查询时,结果集将保持不变,直到我们执行新查询为止。使用这两个软件包,只要更新源,结果集就会更新。 (对性能不利,对实时更新有利)
例子
var theSource = new ContinuousCollection<Customer>(); var theResultSet = from item in theSource where item.Age > 25 select item; //theResultSet.Count would equal 0.
因为我们使用的是Bindable或者Continuous LINQ,所以我们可以修改Source,并且ResultSet将自动包括新项。
theSource.Add(new Customer("Bob", "Barker" , 35, Gender.Male)); //Age == 35 //theResultSet.Count would now equal 1.
可绑定的LINQ提供的其他问题:(直接从自己的页面引用)
contactsListBox.ItemsSource = from c in customers where c.Name.StartsWith(textBox1.Text) select c;
Bindable LINQ will detect that the query relies on the Text property of the TextBox object, textBox1. Since the TextBox is a WPF control, Bindable LINQ knows to subscribe to the TextChanged event on the control. The end result is that as the user types, the items in the query are re-evaluated and the changes appear on screen. No additional code is needed to handle events.
要记住的另一件事是,尽管BindableLinq在LINQ语句中需要" .AsBindable()"调用,但CLINQ要求我们使用ContinuousCollection <T>而不是ObservableCollection <T>。简要介绍了两者之后,我认为我将使用可绑定的LINQ。
的确; Continuous LINQ的主要问题是无法使用实现通用IEnumerable和INotifyCollectionChanged的任何集合。使用实现两个接口的自定义集合,可绑定LINQ没问题。
我可以请我们注意另一个Codeplex项目吗?它称为Obtics,并且处理相同的问题(http://obtics.codeplex.com)。
它既解决了第一个问题,又解决了第二个问题,并使反应性达到了非常深的层次(已进行了基于LINQ的raytracer演示)。
它要求完全支持所有LINQ语句和Enumerable类的方法。
它使用另一种机制来创建实时查询:
var theResultSet = ExpressionObserver.Execute( () => from item in theSource where item.Age > 25 select item ).Cascade();