Convert to C#

Can anyone help me in converting this sql code into C# ? Thanks in advance.

Case When ttInvcHeadRow.CustNum = 17729 then ttInvcHeadRow.CustNum
When ttInvcHeadRow.CustNum = 17729 then ttInvcHeadRow.CustNum
When ttInvcHeadRow.CustNum = 5770 then ttInvcHeadRow.CustNum
When ttInvcHeadRow.CustNum = 5773 then ttInvcHeadRow.CustNum
When ttInvcHeadRow.CustNum = 7062 then ttInvcHeadRow.CustNum
When ttInvcHeadRow.CustNum = 7083 then ttInvcHeadRow.CustNum
When ttInvcHeadRow.CustNum = 7413 then ttInvcHeadRow.CustNum
When ttInvcHeadRow.CustNum = 5775 then ttInvcHeadRow.CustNum
When ttInvcHeadRow.CustNum = 5784 then ttInvcHeadRow.CustNum
else 0
End

The CASE statement is like the switch statement in c#.

1 Like

Like a LINQ statement or actual just c# logic

1 Like

Or a “switch-less” version:


var list1 = new List<Int32>(){17729 , 5770, 5776, 7062, 7083, 7413, 5775, 5784};

custNum = (list1.IndexOf(ttInvcHeadRow.CustNum) != -1 ? ttInvcHeadRow.CustNum : 0);

I am googling to try and figure out how to write it. Just a bit slow at this.

image001.png

Calvin,

I tried this in the code box but it said too many arguments.

image001.png

Google “c# case statement” but honestly the use of a switch in your use case is not necessary, because all of your conditions resolve the same. A single if statement like what @ckrusen posted would work fine. Below would be the code in a “standalone” situation

Use https://dotnetfiddle.net/ as you learn and explore more

using System;
using System.Collections.Generic;
					
public class Program
{
	public static void Main()
	{
		int CustNum = 5770;
		var list1 = new List<Int32>(){17729 , 5770, 5776, 7062, 7083, 7413, 5775, 5784};
		var output = (list1.IndexOf(CustNum) != -1 ? CustNum : 0);
		Console.WriteLine("Match: " + output.ToString());
		
		CustNum = 1111;
		output = (list1.IndexOf(CustNum) != -1 ? CustNum : 0);
		Console.WriteLine("No Match: " + output.ToString());
	}
}

Where is this being used? Somewhere that needs a single expression - like a BPM’s Set Argument/Variable widget?

edit

If so,

ttInvcHeadRow.CustNum == 17729 ? ttInvcHeadRow.CustNum :
  (ttInvcHeadRow.CustNum == 5770 ? ttInvcHeadRow.CustNum : 
  (ttInvcHeadRow.CustNum == 5776 ? ttInvcHeadRow.CustNum : 
  (ttInvcHeadRow.CustNum == 7062 ? ttInvcHeadRow.CustNum :
  (ttInvcHeadRow.CustNum == 7083 ? ttInvcHeadRow.CustNum :
  (ttInvcHeadRow.CustNum == 7413 ? ttInvcHeadRow.CustNum :
  (ttInvcHeadRow.CustNum == 5775 ? ttInvcHeadRow.CustNum :
  (ttInvcHeadRow.CustNum == 5784 ? ttInvcHeadRow.CustNum : 0)))))))

But that’s WAY to much hard coding (the specific CustNums). You really should add a UD field to the Customer table to identify these specific customers. Then use LINQ to see if that field is set. If so, then use the CustNum, else 0.

Edit #2

A shorter (but not so readable) option

(Array.IndexOf(new int[] {17729 , 5770, 5776, 7062, 7083, 7413, 5775, 5784}, ttInvcHeadRow.CustNum) != -1 ? ttInvcHeadRow.CustNum : 0)
1 Like

Yes you are correct. I have a set variable widget called MyCustNum where I am pulling the InvoiceHead.CustNum. Then I have a condition below where I am comparing the MyCustNum value to be equal to 17729 , 5770, 5776, 7062, 7083, 7413, 5775, 5784}. The condition is using and expression.

image001.png

put the following in the expression:

(Array.IndexOf(new int[] {17729 , 5770, 5776, 7062, 7083, 7413, 5775, 5784}, ttInvcHeadRow.CustNum) != -1 ? ttInvcHeadRow.CustNum : 0)

1 Like

Thanks a million !!! I wish I knew how you all learn this stuff. I would love to get myself out of the beginner class.

image001.png

I had no idea how to do it until you asked. And that last solution(Array.IndexOf(....)) took some trial and error.

To be honest, I just have “the knack” for computer related things. I get really lucky at guessing how to do something. Usually the first guess doesn’t work, but puts me in the ball park. And then my “luck” at the next guess of what to try gets me even closer. :wink: I think part of it comes with taking the risk at trying things. And I’ve made some really bad mistakes. Back on a Windows NT server I was trying to keep users out, so I disabled access for all accounts. That happend to include my admin account to… whoops…

1 Like

I agree that @ckrusen… you should NOT have these hard coded values. It would be much better to have this data soft coded… either a UD Field in the customer table, OR a UDCode table created to hold a list of customers that have this condition. In either case, you would simply look in the customer table or the UDCode table to find if the value exists, and then proceed. This allows you to be “soft coded” and data driven. If another customer comes along needing this condition, you update the table.

Example, if you added a new boolean UD Field to the customer called SpecialCustomer_c… here is how you would do it:

bool specialCustomer = Db.Customer.Any(
  x==x.CompanyID == ttInvcHeadRow.CompanyID && 
  x.CustNum == ttInvcHeadRow.CustNum && 
  x.specialCustomer_c == true);
if (specialCustomer) {do something here};

It would be slightly more complex if using a UDCode entry, but not much. In fact, you could load the customer ID (instead of Customer NUMBER) into the UDCode table, making it a little more user friendly for future maintenance.

way too many hardcoded values. You could even use the UserCode table to maintain those customers