我有一个GridView,其中每个标题都有下拉框用于过滤。加载时,每个过滤器都装入其列的不同值。在运行时,我添加“ALL”以允许用户从该字段中选择所有内容。我正在尝试动态构建linq语句以忽略如果下拉框设置为“ALL”的字段。这可行吗?我想看看是否可以在一条语句中完成。下面的示例仅显示了2个下拉框,但我的实际情况最多有5个。
如果选择使用if then语句,则最终会导致混乱的代码。
如果选择使用if then语句,则最终会导致混乱的代码。
DropDownList drpOwners = this.grdOtherQuotes.HeaderRow.FindControl("drpOwners") as DropDownList;
DropDownList drpCompanyName = this.grdOtherQuotes.HeaderRow.FindControl("drpCompanyName") as DropDownList;
var filteredList = (from x in allQuotes
where (drpOwners.SelectedValue != ALL) ? x.SalesRepFullName == drpOwners.SelectedValue:true
&& drpCompanyName.SelectedValue != ALL ? x.CompanyName == drpCompanyName.SelectedValue: true
select x);