我是动态数据的新手,一般来说是反应式扩展世界,我目前面临以下问题,我想在运行时通过使用动态数据包和响应式来更改IObservable<Func<T,bool>>
谓词.NET(C#)中的扩展名。
考虑以下情况,我有一个DataGrid,其中某些列的类型为[[integer,让我们说A,B,C。此外,还有一个过滤器UI,用户可以在其中添加多个过滤器,例如A == 6或过滤器表达式的组合,例如A == 7 ||。 A == 3 || B == 5等,因此基本上我的返回Func<T, bool>
委托的方法如下所示:
private Func<T, bool> FilterOnA(string id)
{
return n => n.Id == int.Parse(id);
}
以及数据管道中的Filter方法调用:
// sourceList is used to fill the ReadOnlyObservableCollection<T> while receiving data from an event pattern sourceList.Connect() // SourceList<T> .Filter(filterViewModel.FilterOnA) .Bind(out _itemsBinding) // private ReadOnlyObservableCollection<T> .DisposeMany() .Subscribe();
如上所述,用户将能够添加/删除/修改,并且更重要的是将过滤器表达式全部组合在一起。由于动态数据过滤器方法采用
Func<T,bool>
或IObservable<Func<T,bool>>
,一种可能的解决方案可能看起来像这样:
public IObservable<Func<T,bool>> Filter1 {get;} public IObservable<Func<T,bool>> Filter2 {get;} public IObservable<Func<T,bool>> Filter3 {get;} public IObservable<Func<T,bool>> FilterX {get;} public IObservable<Func<T,bool>> AllFiltersCombined => Filter1.CombineLatest(Filter2,Filter3,FilterX, (f1,f2,f3,fx) => AggregatePredicatesAnd(f1,f2,f3,fx)); public static Func<T,Bool> AggregatePredicatesAnd(params Func<T,bool>[] predicates) { return predicates.Aggregate<Func<T,bool>>((fa,fb) => (T t) => fa(t) && fb(t)); }
现在,我的问题是,如何以更通用的方式编写此代码?如何结合例如?的组合0 to n
过滤器?以及不同的过滤器类型如何?A <= 7 && A!= 5