如何向linq查询添加多个可选参数

时间:2013-12-19 17:03:51

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

我必须在函数中检查并添加可选参数,但是它需要花费很长的代码才能通过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:切换声明:

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语句是更好的选项,如何缩短代码来捕获可选参数然后我将如何从Case获得var query: 任何帮助非常感谢。

1 个答案:

答案 0 :(得分:2)

你能做的是。

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;

或者我们以前也做过或做过声明。

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

所以,如果值为空,则只是通过,或者如果有值,则检查。