关于使用 IQueryable 对象的快速问题。我可以做这样的事情吗?如果不能 - 另一种方法是什么?
IQueryable<Item> q = this.ObjectContext.items;
q = q.Where(i => i.price > 100);
...
q = q.Where(i => i.class_name.Contain("apple"));
更新:这是我的函数的完整代码(可能是我遗漏了一些东西):
public IQueryable<item> Getitem(string filter)
{
// filtering by user departments
IQueryable<item> pre_q = from t_item in this.ObjectContext.items
join t_useritems in this.ObjectContext.user_to_item on
t_item.number equals t_useritems.number
where t_useritems.user_name == this.userName
select t_item;
if (filter != null)
{
QueryFilter qf = new QueryFilter(filter);
string _excludeClass = qf.GetPropertyValueByName("excludeClass");
string _excludeSubcat = qf.GetPropertyValueByName("excludeSubcat");
string _searchBy = qf.GetPropertyValueByName("searchby");
string _search = qf.GetPropertyValueByName("search");
if (!string.IsNullOrEmpty(_excludeClass))
pre_q = pre_q.Where(i => i.class_name != _excludeClass);
if (!string.IsNullOrEmpty(_excludeSubcat))
pre_q = pre_q.Where(i => i.subcat_name != _excludeSubcat);
if (!string.IsNullOrEmpty(_searchBy) && !string.IsNullOrEmpty(_search))
{
if (_searchBy == "number")
pre_q = pre_q.Where(i => i.number.Contains(_search));
else if (_searchBy == "description")
pre_q = pre_q.Where(i => i.description.Contains(_search));
else if (_searchBy == "class_name")
pre_q = pre_q.Where(i => i.class_name.Contains(_search));
else if (_searchBy == "cat_name")
pre_q = pre_q.Where(i => i.cat_name.Contains(_search));
else if (_searchBy == "subcat_name")
pre_q = pre_q.Where(i => i.subcat_name.Contains(_search));
else if (_searchBy == "all_fields")
pre_q = pre_q.Where(i => i.number.Contains(_search) ||
i.description.Contains(_search) ||
i.class_name.Contains(_search) ||
i.cat_name.Contains(_search) ||
i.subcat_name.Contains(_search));
}
}
}
return pre_q.OrderBy(i => i.number);
}
基本上发生了什么 - 我通过 excludeClass 过滤器和一些搜索文本过滤器。应用 excludeClass 后,我可以看到记录数是正确的,但是当我应用搜索过滤器后 - 它基本上重置了已过滤的项目并且它们正在返回结果中。