Simplifying conditional statements for transaction code I have below code in one function:

public void func()
{
    if (!string.IsNullOrEmpty(customerpay) &amp;&amp; !string.IsNullOrEmpty(warrantypay) &amp;&amp; !string.IsNullOrEmpty(maintanceplan))
    {
        if (customerpay == "Include" &amp;&amp; warrantypay == "Include" &amp;&amp; maintanceplan == "Include")
        {
            transactiontype = "," + "[Population].[Transaction Type Code].&amp;[C^W^I]";
        }
        else if (customerpay == "Include" &amp;&amp; warrantypay == "Include" &amp;&amp; maintanceplan != "Include")
        {
            transactiontype = "," + "[Population].[Transaction Type Code].&amp;[C^W]";
        }
        else if (customerpay == "Include" &amp;&amp; warrantypay != "Include" &amp;&amp; maintanceplan != "Include")
        {
            transactiontype = "," + "[Population].[Transaction Type Code].&amp;[C]";
        }
        else if (customerpay != "Include" &amp;&amp; warrantypay == "Include" &amp;&amp; maintanceplan == "Include")
        {
            transactiontype = "," + "[Population].[Transaction Type Code].&amp;[W^I]";
        }
        else if (customerpay != "Include" &amp;&amp; warrantypay != "Include" &amp;&amp; maintanceplan == "Include")
        {
            transactiontype = "," + "[Population].[Transaction Type Code].&amp;[I]";
        }
        else if (customerpay != "Include" &amp;&amp; warrantypay == "Include" &amp;&amp; maintanceplan != "Include")
        {
            transactiontype = "," + "[Population].[Transaction Type Code].&amp;[W]";
        }
        else if (customerpay == "Include" &amp;&amp; warrantypay != "Include" &amp;&amp; maintanceplan == "Include")
        {
            transactiontype = "," + "[Population].[Transaction Type Code].&amp;[C^I]";
        }
    }
}


I need to simplify the above if-else C# code. Is there a way to do this?
You could do it like this:

public void func()
{
    if (string.IsNullOrEmpty(customerpay) || string.IsNullOrEmpty(warrantypay) || string.IsNullOrEmpty(maintanceplan))
        return;

    if (customerpay != "Include" &amp;&amp; warrantypay != "Include" &amp;&amp; maintanceplan != "Include")
        return;

    List&lt;char&gt; parts = new List&lt;char&gt;();
    if (customerpay == "Include")
        parts.Add("C");
    if (warrantypay == "Include")
        parts.Add("W");
    if (maintanceplan == "Include")
        parts.Add("I");

    transactiontype = ",[Population].[Transaction Type Code].&amp;[" + string.Join("^", parts) + "]";
}


First, we simply return if any of the strings is empty; by inverting the logic, we can reduce one indentation level that makes the code unecessarily complicated. We also return for the single case that isn’t handled in your original code: If all three strings are not "Include".

After that, it’s easy once you find out the pattern of your string. The only changing part of your result string is in the brackets at the end. There are three possible characters: C, W, and I. They appear if the corresponding string equals to "Include". The characters are separated by a ^. So we just check each condition alone, and collect the characters in a list. Then we join with the separation character and build the final result string.