I often use null propagating operator in my code because it gives me more readable code, specially in long queries I don't have to null-check every single class that is used.
The following code throws a compile error that we can't use null propagating operator in lambda.
var cnt = humans.AsQueryable().Count(a => a.House?[0].Price == 5000);
The error :
Error CS8072 An expression tree lambda may not contain a null propagating operator.
C# Could easily translate above code to the code to following code if really can't do anything else!
var cnt = humans.AsQueryable().Count(a => a.House != null && a.House[0].Price == 5000);
I'm curious why C# does nothing and simply throws a compiler error?
Foo?.Bar
is not equivalent toFoo != null ? Foo.Bar : null
becauseFoo
is evaluated once with the null-propagating operator, and twice with the conditional, so the translation wouldn't be correct in all cases. – Dunlinvar q = from c in Categories join p in Products on c equals p.Category into ps from p in ps.DefaultIfEmpty() select new { Category = c, ProductName = (p?.ProductName)??"(No products)"};
instead of having to writeProductName = (p == null) ? "(No products)" : p.ProductName
because EF currently does not support the?.
operator. – Resolve