Is there any way to do this, assign a value within a List.ForEach() statement?

后端 未结 4 713
臣服心动
臣服心动 2021-01-22 22:08

I have this:

var lineArray = line.Split(\';\');

lineArray.ToList().ForEach(x =>
{
    if (x == \"(null)\")
        x = \"NULL\";
    else
        x = string.         


        
4条回答
  •  佛祖请我去吃肉
    2021-01-22 23:02

    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:enter image description here

    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

    enter image description here

提交回复
热议问题