How do I use IF/ELSE or CASE In DataColumn.Expression?

青春壹個敷衍的年華 提交于 2019-12-23 09:14:47

问题


I have a table with 1 column: 'Status' I want to add in another column named 'Action', its value will be as follow: if Status = 'Yes' Then Action = 'Go', otherwise, Action = 'Stop'. I used this following code to add in column 'Action' but it didn't work:

myDataTable.Columns.Add("Action", typeof(string), "IF [Status] = 'Yes' THEN 'Go' ELSE 'Stop' END");

回答1:


The expression you are looking for is:

IIF( [Status] = 'Yes', 'Go', 'Stop' )

DataTables do not support standard SQL CASE statements, nor do they support "IF... ELSE" statements. You must use the inline-if function: IIF

See DataColumn.Expression Property (MSDN)



来源:https://stackoverflow.com/questions/19325064/how-do-i-use-if-else-or-case-in-datacolumn-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!