【问题标题】:How do I add multiple optional parameters to linq query如何将多个可选参数添加到 linq 查询
【发布时间】:2013-12-19 17:03:51
【问题描述】:

我必须检查并在函数中添加可选参数,但它需要很长的代码才能通过 IF-Else 语句,如果我选择 switch 语句,则无法将 'var' 变量转换为字符串错误不断出现,我该如何检查并向 linq 查询添加可选参数,请帮助。这是我的代码。 (我将同时提供一个带有 If 语句和另一个带有抛出错误的 switch 语句)

代码 1 If-else 语句:

public static void loadGrid(ref GridView gvMain, string cID, string desig = "All", string importancy = "All", string status = "All")
{

    int _cId = Convert.ToInt32(cID);
    List<clsclass> list = new List<clsclass>();
    var db = new MyEntities();

    if (_cId == 0 && desig == "All" && importancy == "All" && status == "All")
    {
        var query = from table in db.Members select table;
        list.Clear();
        foreach (var item in query)
        {
            clsclass ctl = new clsclass();
            ctl.Id = Convert.ToInt32(item.LID);
            ctl.Name = item.FirstName + " " + item.LastName;
            ctl.Gender = item.Gender;
            ctl.Age = Convert.ToInt32(item.Age);
            ctl.Mobile = item.Mobile;
            ctl.Workphone = item.WorkPhone;
            ctl.Designation = item.Designation;
            ctl.Importancy = item.Importancy;
            list.Add(ctl);
        }
    }
    else if (_cId != 0 && desig == "All" && importancy == "All" && status == "All")
    {
        var query = from table in db.Members where table.CID == _cId select table;
        list.Clear();
        foreach (var item in query)
        {
            clsclass ctl = new clsclass();
            ctl.Id = Convert.ToInt32(item.LID);
            ctl.Name = item.FirstName + " " + item.LastName;
            ctl.Gender = item.Gender;
            ctl.Age = Convert.ToInt32(item.Age);
            ctl.Mobile = item.Mobile;
            ctl.Workphone = item.WorkPhone;
            ctl.Designation = item.Designation;
            ctl.Importancy = item.Importancy;
            list.Add(ctl);
        }
    }
    //AND SO ON I HAVE TO CHECK THE OPTIONAL PARAMETERS...... 
    //else if()
    //{
    //}
}

并且在下面的代码中,如果我尝试使用 switch 语句来绑定基于查询的条件,它会抛出错误:

代码2:switch语句:

public static void LoadGrid(ref GridView gvMain, string cID, string desig = "All", string importancy = "All", string status = "All")
{

    int _cId = Convert.ToInt32(cID);
    List<clsclass> list = new List<clsclass>();
    var db = new MyEntities();
    var query;
    switch (query)
    {
        case _cId == 0 && desig == "All" && importancy == "All" && satus == "All":
            query = from b in db.ConstituencyLeaders select b;
        case _cId != 0 && desig == "All" && importancy == "All" && satus == "All":
            query = from b in db.ConstituencyLeaders where b.ConstituencyID == _cId select b;
    }
    foreach (var item in query)
    {
        clsclass cl = new clsclass();
        cl.LeaderId = item.LID;
        //...remaining members add 
        list.Add(cl);
    }
    gvMain.DataSource = list;
    gvMain.DataBind();
}

所以基本上我有两个问题如何缩短代码以捕获可选参数如果 switch 语句是更好的选择那么我将如何从案例中实现var query: 非常感谢任何帮助。

【问题讨论】:

    标签: c# linq entity-framework c#-4.0 optional-parameters


    【解决方案1】:

    你能做的是。

    var query = db.Members.AsQuerryable();
    
    if(_cid != "")
    {
        query = query.where(table => table.CID == _cId);
    }
    

    然后在你的陈述中使用它

    var result = from table in query where table.CID == _cId select table;
    

    或者我们也习惯做or语句。

     var query= from table in query where (table.CID == _cId || _cId = "") select table;
    

    所以如果值是空的,它就会通过,或者如果有一个值它会检查。

    【讨论】:

    • 谢谢,db.Members.AsQuerryable() 帮助我摆脱了每个 if-else 语句块中的所有 foreach 循环,有没有我可以检查选项值设置为的地方“全部”与否,如果其中一个不是“全部”或其中两个未设置为“全部”而不是遍历所有 if 语句,则将它们添加到查询中...
    • 嗯,我想不出一个简单的方法,但可能值得一试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    • 2019-04-27
    • 2019-05-30
    相关资源
    最近更新 更多