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
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
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.
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.
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. 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…
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:
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.