[英]Replace string values with LINQ
I have the following string containing column names: 我有以下包含列名称的字符串:
string colsToSelect;
This string contains column names (eg: col1+col2+col3+col4+col5 or col1+col3) and in those columns there are numerical values. 该字符串包含列名称(例如:col1 + col2 + col3 + col4 + col5或col1 + col3),在这些列中有数值。
I am using this to get the total of all these columns from database. 我使用它来从数据库中获取所有这些列的总和。
SELECT " + colsToSelect + " AS totalVolume " + "FROM myTable
However I need to add nvl(col1,0) to each of the columns as those columns may contain null. 但是我需要将nvl(col1,0)添加到每个列,因为这些列可能包含null。
I have achived it like this: 我这样做了:
colsToSelect = colsToSelect.Replace("+", "+nvl(");
colsToSelect = colsToSelect.Replace("+", ",0)+");
colsToSelect = "nvl(" + colsToSelect;
colsToSelect = colsToSelect + ",0)";
This gives me nvl(col1, 0) + nvl(col2, 0)
etc.. but I was wondering if there is a better way of achieving this, possibly with LINQ or with sql? 这给了我nvl(col1, 0) + nvl(col2, 0)
等。但我想知道是否有更好的方法来实现这一点,可能使用LINQ或sql?
You can use string.Split
, then string.Join
again: 您可以使用string.Split
,然后string.Join
再次:
colsToSelect = string.Join("+",
colsToSelect.Split('+')
.Select(x => string.Format("nvl({0}, 0)", x)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.