问题描述
使用Entity Framework 4.0
Using Entity Framework 4.0
我有这样的搜索条件
有四个字段,允许用户过滤搜索.条件全部为AND
.如果文本框值为String.Empty
或下拉列表值为全部,则结果必须省略相应的过滤器.可以在存储过程中执行此操作,但是在Linq2SQL/Entity Framework方案中根本无法模仿.
There are four fields that allow the users to filter their search. The conditions are all AND
. The result has to omit the corresponding filter if the textbox value is String.Empty
or the dropdownlist value is All. Could do this in a Stored Procedure but I am unable to mimic that at all in a Linq2SQL/ Entity Framework scenario.
我的问题是,如何省略IEnumerable.根据输入的值在Linq中的什么位置?
My question is this, how to omit IEnumerable.Where in the Linq according to some entered values?
推荐答案
您可以链接where子句.您只需要一个IQueryable数据源.
You can chain your where clauses. You just need an IQueryable datasource.
var filteredData = _repository.GetAll();
//If your data source is IEnumerable, just add .AsQueryable() to make it IQueryable
if(keyWordTextBox.Text!="")
filteredData=filteredData.Where(m=>m.Keyword.Contains(keyWordTextBox.Text));
if(LocationDropDown.SelectedValue!="All")
filteredData=filteredData.Where(m=>m.Location==LocationDropDown.SelectedValue));
... etc....
由于它是IQueryable的,因此只有在绑定数据后才提取数据,因此它只会提取所需的数据.
Because it is IQueryable, the data is not fetched until you bind it so it only pulls the data you need.
这篇关于EF中的多重条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!