使用3个下拉列表过滤IEnumerable

本文关键字:IEnumerable 过滤 下拉列表 3个 使用 | 更新日期: 2023-09-27 18:03:50

我有一个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是否有选择

使用3个下拉列表过滤IEnumerable

哎呀,为什么上市后才解决这些事情!!

// 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();