[ACCEPTED]-How to replace null by "NULL" in a List<string> without foreach loop?-list
src.Select(s => s ?? "NULL").ToList();
But what's wrong with using a foreach loop?
0
var list = new List<string>() { null, "test1", "test2" };
for (int i = 0; i < list.Count; i++)
{
if (list[i] == null)
{
list[i] = "NULL";
}
}
No foreach
.
EDIT:
As no one seems to understand the 10 meaning of this answer: LINQ does a foreach
loop 9 internally. You want to conditionally modify 8 each item of a list? Then you have to enumerate 7 it.
LINQ is here to help us write queries. Oh wait, this is the Q of LINQ.
LINQ 6 is NOT here to modify existing lists. Just 5 use a good old for loop
here. You could of course 4 create a new list based on the existing one with 3 modified values (see best-voted answer), but 2 I'm afraid you'll start using LINQ in a 1 wrong way.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.