I have this:
var lineArray = line.Split(\';\');
lineArray.ToList().ForEach(x =>
{
if (x == \"(null)\")
x = \"NULL\";
else
x = string.
you can construct a new list from your original one :
var newList = lineArray.Select(x => x == "(null)" ? "NULL" : string.Format("'{0}'", x));
don't use ForEach
like that - use a for
loop.
for (int i = 0; i < lineArray.Length; i++)
{
if (lineArray[i] == "(null)")
lineArray[i] = "NULL";
else
lineArray[i] = string.Format("'{0}'", lineArray[i]);
}
I think this small change will work
var lineArray = line.Split(';').ToList();
lineArray.ForEach(x =>
{
if (x == "(null)")
x = "NULL";
else
x = string.Format("'{0}'", x);
});
Below is a working example
string line = "a;b;null;c;";
var lineArray = line.Split(';').ToList();
lineArray.ForEach(x =>
{
if (x == "(null)")
x = "NULL";
else
x = string.Format("'{0}'", x);
});
Result:
If you need just to remove (null) values from the list, you do not need to loop just use removeAll
lineArray.RemoveAll(a=>a.Equals("(null)"));
working example below
var lineArray = line.Split(';')
.Select(x=>x == "(null)"
? "NULL"
: string.Format("'{0}'", x))
.ToArray();
you are trying to use List<T>.ForEach(Action<T> action)
with lambda expression (T is string here)
if lambda expression is replaced with named method it turns out that only method argument is modified, but changes are not reflected on calling side, because x
is not ref
argument
private void Replace(string x)
{
if (x == "(null)")
x = "NULL";
else
x = string.Format("'{0}'", x);
}
var list = lineArray.ToList();
list.ForEach(Replace);
// check list here and make sure that there are no changes
ForEach could work if T is a reference type and action modifies some properties but not the reference itself