Reputation: 9225
I have two dropdownlist:
<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>
How can I set up LINQ to do the following:
I did the following:
GridView1.DataSource = List1
.Where(en =>
en.howManyTimes == (dropdownlist1.SelectedIndex > 0 ? dropdownlist1.SelectedItem.Value : ""))
.Select(en => new { en.TheID, en.GetFile, en.GetLink });
I am just not sure after the :
in the ternary operator.
This is what I would like:
GridView1.DataSource = List1.Where(en => en.howManyTimes == (if dropdownlist1 selected index is great than 0 then use that value for howManyTimes, otherwise select any howManyTimes) && en.whatDate == (if dropdownlist2 selected index is great than 0 then use that value for whatDate, otherwise select any whatDate)).Select(en => new { en.TheID, en.GetFile, en.GetLink });
Can I please get some assistance in completing it.
Upvotes: 1
Views: 75
Reputation: 109119
If Any of the selected index is greater than 0 then use the condition otherwise select all.
You can achieve this by making the predicate evaluate to something that's always true when dropdownlist1.SelectedIndex <= 0
:
GridView1.DataSource = List1
.Where(en => dropdownlist1.SelectedIndex <= 0
|| en.howManyTimes == dropdownlist1.SelectedItem.Value)
.Select(en => new { en.TheID, en.GetFile, en.GetLink });
Upvotes: 1