1

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, The db.Members.AsQuerryable() help me to get rid of all the foreach loops from each if-else statement blocks, Is there anyway where I can check for options values are set to "All" or not and add them in query if one of them is not "All" or two of them are not set to "All" instead of going through all if-statement...
Hmm there is not an easy way I can think of doing that, but might be worth a try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.