我有一个ID列表,我需要根据3个下拉框中选择的值进行过滤。我让它工作了2个下拉菜单,但可以看出这不是完成它的最好方法:
var nodesList = new List<int>();
// Check each item in search output against the values in the dropdown boxes
bool found = false;
foreach (var item in searchOutput) // searchOut is an IEnumerable
{
// This is where I need to add another dropdown box option but realise the code below is going to get even messier !!
if (GetNodeProperty(item.Id, "sportChooser", "sportChooser").Contains(ddlSport.SelectedValue))
{
if (ddlCategory.SelectedIndex == 0 || GetNodeProperty(item.Id, "categoryChooser", "categoryChooser").Contains(ddlCategory.SelectedValue))
{
found = true;
}
}
if (GetNodeProperty(item.Id, "categoryChooser", "categoryChooser").Contains(ddlCategory.SelectedValue))
{
if (ddlSport.SelectedIndex == 0 || GetNodeProperty(item.Id, "sportChooser", "sportChooser").Contains(ddlSport.SelectedValue))
{
found = true;
}
}
if (found)
{
nodesList.Add(item.Id);
found = false;
}
}
lvSearchResult.DataSource = nodesList;
lvSearchResult.DataBind();
}
我认为某种形式的Lambda表达会更合适,但对于我的生活似乎无法让它发挥作用。这就是我到目前为止所做的:
foreach (var item in searchOutput)
{
nodesList.Add(item.Id);
}
List<int> filteredNodes = nodesList
.Where(
x =>
GetNodeProperty(Convert.ToInt32(x.ToString()), "categoryChooser", "categoryChooser").Contains(ddlCategory.SelectedValue))
.Where(
x =>
GetNodeProperty(Convert.ToInt32(x.ToString()), "sportChooser", "sportChooser").Contains(ddlSport.SelectedValue))
.ToList();
注意:我需要通过3个下拉列表的组合来过滤searchOutput,无论是否选择了某些内容。
答案 0 :(得分:0)
Grrr,你为什么要在上市后解决这些问题!!
// Check each item in search output against the values in the dropdown boxes
foreach (var item in searchOutput)
{
nodesList.Add(item.Id);
}
List<int> filteredNodes = nodesList
.Where(
x =>
((ddlCategory.SelectedIndex == 0 || GetNodeProperty(Convert.ToInt32(x.ToString()), "categoryChooser", "categoryChooser").Contains(ddlCategory.SelectedValue)) && (ddlSport.SelectedIndex == 0 || GetNodeProperty(Convert.ToInt32(x.ToString()), "sportChooser", "sportChooser").Contains(ddlSport.SelectedValue))) && (ddlFileType.SelectedIndex == 0 || ContentType(Convert.ToInt32(x.ToString())) == ddlFileType.SelectedValue.ToLower()))
.ToList();
lvSearchResult.DataSource = filteredNodes;
lvSearchResult.DataBind();