【发布时间】:2018-06-21 09:02:04
【问题描述】:
我正在尝试对列表执行查询,以使用在代码中其他位置设置的表达式提供即时结果,而第二个线程关闭并使用它从 Linq 中的数据库获取完整的结果集查询。
我知道表达式本身没问题,因为当我通过线路将它发送到服务器端并将其应用于IQueryable 时,它就会起作用。但是,在客户端应用时会产生以下错误:
System.InvalidOperationException:从范围“”引用的“MissionControlSuite.Shared.Interface.Model.IBox”类型的“变量”项,但未定义”
执行过滤的代码:
public abstract class GridWithPaging<T> where T : class
{
List<T> _items
public Expression<Func<T, bool>> Filter { get; set; }
public ObservableCollection<IGridFilter<T>> Filters { get; set; }
// more code skipped for breveity
public void FiltersChanged(object sender, EventArgs e)
{
Expression<Func<T, bool>> combined = item => true;
foreach (var filter in Filters)
combined = Expression.Lambda<Func<T, bool>>(
Expression.AndAlso(filter.Expression.Body, combined.Body),
combined.Parameters.Single());
Filter = combined;
NotifyPropertyChanged("Filter");
}
public void AddFilter(IGridFilter<T> filter)
{
Filters.Add(filter);
NotifyPropertyChanged("Filters");
}
private void DoFiltering(int start)
{
var newView = _items.Skip(start);
if(Filter != null)
newView = newView.AsQueryable().Where(Filter);
//some more code that acts on the filtered list
}
}
表达式在其他地方设置如下:
Expression<Func<IBox, bool>> expression = item => item.BoxId.Contains(v);
var filter = new GridFilter<IBox>()
{
Name = string.Format("{0} contains {1}", Property, Value),
Expression = expression
};
Grid.AddFilter(filter);
【问题讨论】:
-
FiltersChanged 方法需要从 sender 参数中获取数据,而不是从 Filters 中获取数据。
标签: c# linq expression expression-trees linq-expressions