Linq where子句绑定到dropdownlist selectedvalue

时间:2011-06-15 20:04:51

标签: c# asp.net linq

我有一个包含3个值的dropDownList1:N,U,All。我有一个由Linq语句填充的dropDownList2,其值通过在SelectedIndexChanged事件期间在第一个下拉列表中进行的选择进行过滤。

string theType = dropDownList1.SelectedValue;

var yearQuery = (from y in context
where y.Type == theType
orderby y.Year ascending
select y.Year).Distinct();

但是,第一个列表中的“全部”选项不是数据集的“类型”列中的值之一。当有人在下拉列表中选择“全部”时,我会喜欢它,它会返回所有记录。是否有等效的*运算符?有什么想法吗?

4 个答案:

答案 0 :(得分:1)

试试这个:

string theType = dropDownList1.SelectedValue;

var yearQuery = (from y in context                    
                orderby y.Year ascending
                select y.Year).Distinct();

if(theType != "All")
    yearQuery = yearQuery.Where(p=>p.Type == theType);

答案 1 :(得分:0)

启动此操作的最简单方法是分支代码,然后设置yearQuery,当人选择“all”时,没有类型过滤器。您可以稍后重构一个更简洁的选项。

答案 2 :(得分:0)

var yearQuery =(from y in context where(y.Type == theType或theType ==“ALL”)orderby y.Year ascending select y.Year).Distinct();

答案 3 :(得分:0)

string theType = dropDownList1.SelectedValue;

var yearQuery = (from y in context
where theType == "All" || y.Type == theType
orderby y.Year ascending
select y.Year).Distinct();

这也应该有用。