0

我有一个学习项目,其中数据网格由 3 个控件(一个复选框和 2 个下拉菜单)过滤

我即将结束并继续另一个项目,因为它运行良好,但我不喜欢嵌套 IF 语句以捕获 3 个过滤器的所有可能组合的复杂性,并且想知道是否有更好的方法。例如:允许轻松添加更多过滤器的东西,而不是遍历所有的巢并添加另一个疯狂级别。

private void BuildQuery()
    {

        EntityQuery<MASTER_DOCKS> query = QDocksContext.GetMASTER_DOCKSQuery();


        if (Tonnage.IsChecked.HasValue && Tonnage.IsChecked.Value)
        {

            if (null != FilterWaterWay.SelectedValue)
            {

                string WaterwaytoFilterBy = FilterWaterWay.SelectedValue.ToString();

                if (!string.IsNullOrWhiteSpace(WaterwaytoFilterBy) && WaterwaytoFilterBy != "[Select WaterWay]")
                {

                    if (null != FilterState.SelectedValue)
                    {

                        string StateToFilterBy = FilterState.SelectedValue.ToString();

                        if (null != FilterState.SelectedValue && !string.IsNullOrWhiteSpace(StateToFilterBy) && StateToFilterBy != "[Select State]")
                        {

                            if (!string.IsNullOrWhiteSpace(StateToFilterBy) && StateToFilterBy != "[Select State]")
                            {

                                query = query.Where(s => s.WTWY_NAME == WaterwaytoFilterBy && s.STATE == StateToFilterBy && (s.Tons != "0" && s.Tons != "")).OrderBy(s => s.WTWY_NAME);


                                MyQuery.Text = "Tonnage, WW and State";

                            }
                        }

                        if (StateToFilterBy == "[Select State]") //waterway but no state
                        {


                            query = query.Where(s => s.WTWY_NAME == WaterwaytoFilterBy && (s.Tons != "0" && s.Tons != "")).OrderBy(s => s.WTWY_NAME);

                            MyQuery.Text = "Tonnage, WW No State";
                        }
                    }
                }

                else 
                {
                    if (null != FilterState.SelectedValue)
                    {

                        string StateToFilterBy = FilterState.SelectedValue.ToString();

                        if (null != FilterState.SelectedValue && !string.IsNullOrWhiteSpace(StateToFilterBy) && StateToFilterBy != "[Select State]")
                        {

                            if (!string.IsNullOrWhiteSpace(StateToFilterBy) && StateToFilterBy != "[Select State]")
                            {
                                query = query.Where(s => s.STATE == StateToFilterBy && (s.Tons != "0" && s.Tons != "")).OrderBy(s => s.WTWY_NAME);
                                MyQuery.Text = "Tonnage State No WW";
                            }

                        }
                        else
                        {
                            query = query.Where(s => (s.Tons != "0" && s.Tons != ""));
                            MyQuery.Text = "Tonnage No State No WW";
                        }
                    }
                }
            }
        }


        else //no tonnage
        {

            if (null != FilterWaterWay.SelectedValue)
            {

                string WaterwaytoFilterBy = FilterWaterWay.SelectedValue.ToString();

                if (!string.IsNullOrWhiteSpace(WaterwaytoFilterBy) && WaterwaytoFilterBy != "[Select WaterWay]")
                {

                    if (null != FilterState.SelectedValue)
                    {

                        string StateToFilterBy = FilterState.SelectedValue.ToString();

                        if (null != FilterState.SelectedValue && !string.IsNullOrWhiteSpace(StateToFilterBy) && StateToFilterBy != "[Select State]")
                        {

                            if (!string.IsNullOrWhiteSpace(StateToFilterBy) && StateToFilterBy != "[Select State]")
                            {

                                query = query.Where(s => s.WTWY_NAME == WaterwaytoFilterBy && s.STATE == StateToFilterBy).OrderBy(s => s.WTWY_NAME);

                                MyQuery.Text = "No Tonnage, WW and State";

                            }
                        }

                        if (StateToFilterBy == "[Select State]") //waterway but no state
                        {

                            query = query.Where(s => s.WTWY_NAME == WaterwaytoFilterBy).OrderBy(s => s.WTWY_NAME);

                            MyQuery.Text = "No Tonnage, WW No State";
                        }
                    }
                }

                else                     
                {

                    if (null != FilterState.SelectedValue)
                    {

                        string StateToFilterBy = FilterState.SelectedValue.ToString();

                        if (null != FilterState.SelectedValue && !string.IsNullOrWhiteSpace(StateToFilterBy) && StateToFilterBy != "[Select State]")
                        {

                            if (!string.IsNullOrWhiteSpace(StateToFilterBy) && StateToFilterBy != "[Select State]")
                            {
                                query = query.Where(s => s.STATE == StateToFilterBy).OrderBy(s => s.WTWY_NAME);
                                MyQuery.Text = "No Tonnage State No WW";
                            }

                        }
                        else
                        {
                            LoadAllData();
                            MyQuery.Text = "No Tonnage No State No WW";
                        }
                    }
                }
            }
        }

        LoadOperation<MASTER_DOCKS> loadOp = this.QDocksContext.Load(query);
        DocksGrid.ItemsSource = loadOp.Entities;
    }
4

1 回答 1

3

与其尝试构建单个.Where(...)调用,不如.Where(...)根据控制设置链接调用:

if (Tonnage.IsChecked.HasValue && Tonnage.IsChecked.Value)
{
    query = query.Where(s => s.Tons != "0" && s.Tons != "")
}

if (null != FilterWaterWay.SelectedValue)
{
    string WaterwaytoFilterBy = FilterWaterWay.SelectedValue.ToString();

    if (!string.IsNullOrWhiteSpace(WaterwaytoFilterBy) && WaterwaytoFilterBy != "[Select WaterWay]")
    {
         query = query.Where(s => s.WTWY_NAME == WaterwaytoFilterBy);
    }
}

if (null != FilterState.SelectedValue)
{
    string StateToFilterBy = FilterState.SelectedValue.ToString();

    if (null != FilterState.SelectedValue && !string.IsNullOrWhiteSpace(StateToFilterBy) && StateToFilterBy != "[Select State]")
    {
         query = query.Where(s => s.STATE == StateToFilterBy);
    }
}

结果,在复选框被选中并且其他两个控件具有有效值的情况下,与以下内容相同:

query = query.Where(s => s.Tons != "0" && s.Tons != "")
             .Where(s => s.WTWY_NAME == WaterwaytoFilterBy)
             .Where(s => s.STATE == StateToFilterBy)
于 2013-11-07T21:53:27.107 回答