Solved

if condition

Posted on 2014-10-14
4
65 Views
Last Modified: 2014-10-18
please code below, i need to use code below less if statements, is look not properway or not very professional to use this much If conditions, i belive may be another way .

            if (ComplainType == "Total")
            {


                SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyTots";
            }


            if (ComplainType == "ManualKit")
            {


                SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyMan
            }


            if (ComplainType == "iComplaints")
            {


                SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyIcom
            }


            if (ComplainType == "InsComplaints")
            {


                SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyINS
            }
0
Comment
Question by:ukerandi
4 Comments
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 167 total points
ID: 40379846
try like this:

switch (ComplainType )
{
    case "Total":
            SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyTots";
        break;
    case "ManualKit":
        SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyMan";
        break;
    case "iComplaints":
           SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyIcom";
        break;
    case "InsComplaints":
                SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyINS";
        break;
    default:
   
        break;
}

Open in new window

0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 167 total points
ID: 40379849
I am not a C# programmer, but I think you need to be looking at switch statement

switch ( ComplainType ) {

     case "Total":
          SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyTots";
          break;

     case "ManualKit":
          SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyMan
          break;

   ... and so on
}

Open in new window


See http://msdn.microsoft.com/en-GB/library/06tc147t.aspx for examples
0
 
LVL 4

Expert Comment

by:javaftper
ID: 40379857
You should use a case statement for faster execution-

// ... Switch on the string.
	switch (ComplainType)
	{
	    case "Total":
                SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyTots";
		break;
	    case "ManualKit":
		  SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyMan";
		break;
	    case "etc":
		etc;
		break;
	}

Open in new window

0
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 166 total points
ID: 40380565
As your SQL statements are all pretty much the same, here's a completely different approach to using switch statements:

Dictionary<string, string> ComplaintTables = new Dictionary<string, string>();

ComplaintTables.Add("Total", "YearlyTots");
ComplaintTables.Add("ManualKit", "YearlyMan");
ComplaintTables.Add("iComplaints", "YearlyIcom");
ComplaintTables.Add("InsComplaints", "YearlyINS");

SqlTotalComplaints = String.Format("select count(*) as YearlyTotalComplaint from NumberOfComplaints_{0}", ComplaintTables[ComplainType]);

Open in new window

Always good to have options ;)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now