Question

I Have to check and add the optional parameter in function but its taking lengthy code to go through IF-Else statement, If I choose switch statement then Cannot convert 'var' variable to string error keep coming up, How do I check and add optional parameters to linq query please help. Here is my code. (I will provide both the one with If statement and other one with switch statement which is throwing error)

Code 1 If-else statement:

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()
    //{
    //}
}

And In below code If I try to use switch statement to bind query based condition its throwing error:

Code 2:Switch statement:

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();
}

So basically I got two questions How to shorten the codes to capture the optional parameters if switch statement is better option then how would I achieve var query from Case: any help much appreciated.

Was it helpful?

Solution

What you can do is.

var query = db.Members.AsQuerryable();

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

Then use that in your statement

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

or we also used to do or statements.

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

so if the value is empty it just goes through or if there is a value it checks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top