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.
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.
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?
你可以使用string.Split
,然后string.Join
再次:
colsToSelect = string.Join("+",
colsToSelect.Split('+')
.Select(x => string.Format("nvl({0}, 0)", x)));
链接地址: http://www.djcxy.com/p/68836.html
上一篇: 如何过滤数据表中的某些DataRows
下一篇: 用LINQ替换字符串值