0

我有一个 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。

4

1 回答 1

0

呸呸呸呸呸,你上市后怎么解决这些事情!!

// 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();
于 2013-03-14T16:18:21.607 回答