使用嵌套if语句进行C#LINQ过滤

时间:2013-11-07 21:44:17

标签: c# silverlight linq-to-entities

我有一个学习项目,数据网格由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;
    }

1 个答案:

答案 0 :(得分: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)