0

I want to write the following statement:

things.ToList().ForEach(x =>
{
    x.thingname = x.thingname.Replace("oldvalue", "newvalue");
    x.thingname2 = x.thingname2.Replace("oldvalue", "newvalue");
});

This code works fine and replace works successfully. However if x.thingname is null the code errors. How can I check for null values before doing the replace?

LordWilmore
  • 2,650
  • 2
  • 22
  • 27
c br
  • 29
  • 3
  • 2
    `if (x.thingname != null) { x.thingname = ... }`? Or alternatively `x.thingname = x.thingname?.Replace(...)` using C# 6.0's [null conditional operator](https://stackoverflow.com/questions/28352072/what-does-question-mark-and-dot-operator-mean-in-c-sharp-6-0) – hnefatl Apr 20 '18 at 12:24
  • https://stackoverflow.com/help/someone-answers – Pranay Rana Apr 20 '18 at 12:36
  • x.thingname = x.thingname?.Replace(....) gives me syntax errors?... – c br Apr 20 '18 at 12:43
  • @cbr if `?.` gives a syntax error, you're probably using an older compiler - that syntax is relatively new (C# 6). So if that isn't an option, use `if(x.thingname != null) { ... }` instead – Marc Gravell Apr 20 '18 at 13:55

3 Answers3

6

that isn't LINQ - it is just lambdas

If you want to check whether something is null: check whether it is null.

if  (x.thingname != null) { ... }

would work, although the inline null check would work too:

x.thingname = x.thingname?.Replace("oldvalue", "newvalue");
x.thingname2 = x.thingname2?.Replace("oldvalue", "newvalue");

However, the ToList().ForEach() here is unnecessary and expensive. Just use foreach:

foreach (var x in things) {
    x.thingname = x.thingname?.Replace("oldvalue", "newvalue");
    x.thingname2 = x.thingname2?.Replace("oldvalue", "newvalue");
}
Marc Gravell
  • 927,783
  • 236
  • 2,422
  • 2,784
  • 1
    you dont need x?. as x must be there , otherwise it will throw execption in first go it self – Pranay Rana Apr 20 '18 at 12:26
  • @PranayRana you're right - I typo'd; fixed – Marc Gravell Apr 20 '18 at 12:26
  • just want to know `ToList().ForEach()` slow down performance ?? – Pranay Rana Apr 20 '18 at 12:37
  • 1
    @PranayRana well, yes! `ToList()` allocates a new list, iterates (`foreach`) over the source, appends to the new list (perhaps resizing it multiple times), and returns the new list - that's a lot of work! Then `ForEach` iterates over the new list, and requires a delegate allocation first (although that might be reused in some cases). It also precludes the possibility of using a custom duck-typed iterator, which several types provide, and which is especially cheap. So yeah, there are *tons* of ways that this make this **much** more expensive than just a simple `foreach` over the original data. – Marc Gravell Apr 20 '18 at 12:44
  • thanks it helps ...i think you should include in answer too that will help readers .. – Pranay Rana Apr 20 '18 at 12:45
  • @PranayRana meh, it is here – Marc Gravell Apr 20 '18 at 12:47
3

Try using the ?. operator:

things.ToList().ForEach(x =>
{
    x.thingname = x.thingname?.Replace("oldvalue",      "newvalue");
    x.thingname2 = x.thingname2?.Replace("oldvalue", "newvalue");
});
fubo
  • 39,783
  • 16
  • 92
  • 127
Slaven Tojic
  • 2,760
  • 2
  • 11
  • 30
1

you can try like this , ? (?. and ?[] null-conditional Operators) (it has nothing to do with linq , you code is ok just put ? as suggested )

 x.thingname =  x.thingname?.Replace("oldvalue",  "newvalue");

and you code will be like this

things.ToList().ForEach(x =>
{
    x.thingname = x.thingname?.Replace("oldvalue",      "newvalue");
    x.thingname2 = x.thingname2?.Replace("oldvalue", "newvalue");
});
Pranay Rana
  • 164,177
  • 33
  • 228
  • 256