C# 如何将LINQ与DropDownList一起使用

C# 如何将LINQ与DropDownList一起使用,c#,linq,drop-down-menu,C#,Linq,Drop Down Menu,我有两个下拉列表: <asp:DropDownList ID="dropdownlist1" runat="server"> <asp:ListItem>Select One</asp:ListItem> <asp:ListItem>once</asp:ListItem> <asp:ListItem>twice</asp:ListItem> <asp:ListItem>

我有两个下拉列表:

<asp:DropDownList ID="dropdownlist1" runat="server">
    <asp:ListItem>Select One</asp:ListItem>
    <asp:ListItem>once</asp:ListItem>
    <asp:ListItem>twice</asp:ListItem>
    <asp:ListItem>thrice</asp:ListItem>
</asp:DropDownList>

<asp:DropDownList ID="dropdownlist2" runat="server">
    <asp:ListItem>Select One</asp:ListItem>
    <asp:ListItem>1/22/2014</asp:ListItem>
    <asp:ListItem>1/25/2016</asp:ListItem>
</asp:DropDownList>
我只是不确定三元运算符中的
之后

这就是我想要的:

GridView1.DataSource=List1.Where(en=>en.howManyTimes==(如果 dropdownlist1选定索引大于0,然后将该值用于 howManyTimes,否则选择任何howManyTimes)&&en.whatDate== (如果dropdownlist2 selected index大于0,则使用该值 对于whatDate,否则选择任意whatDate)。选择(en=>new{ en.TheID、en.GetFile、en.GetLink})

请帮我完成它好吗

如果任何选定索引大于0,则使用条件,否则选择全部

您可以通过使谓词在
dropdownlist1.SelectedIndex dropdownlist1.SelectedIndex new{en.TheID,en.GetFile,en.GetLink})时计算为始终为真的值来实现这一点;

我想把所有的问题都放在一个LINQ查询中,但我认为这是不可能的,嗯?那可能行得通。谢谢你的来信。那么,让另一个dropdownlist与另一个
|
一起添加,还是更容易并建议使用多个
Where()
?我不知道你说的另一个dropdownlist是什么意思。请先试试这是否适合你。然后,如果您需要其他帮助,请尝试使用另一个dropdownlist,并提出一个新问题。实际上,它仍然是同一问题的一部分:)。它似乎正在工作,但第二个下拉列表的另一个条件没有正常工作。我用你的代码更新了我的问题…没关系。。。我只是在的地方使用了多个,效果很好。谢谢你的帮助。
GridView1.DataSource = List1
    .Where(en => 
        en.howManyTimes == (dropdownlist1.SelectedIndex > 0 ? dropdownlist1.SelectedItem.Value : ""))
    .Select(en => new { en.TheID, en.GetFile, en.GetLink });
GridView1.DataSource = List1
    .Where(en => dropdownlist1.SelectedIndex <= 0
              || en.howManyTimes == dropdownlist1.SelectedItem.Value)
    .Select(en => new { en.TheID, en.GetFile, en.GetLink });