C
C#8mo ago
ApathyErr

Why can't I use the null propagation operator in lambda expressions?

using (ProfitCalculatorDataBaseContext db = new ProfitCalculatorDataBaseContext())
{
var customers = db.Customers.ToList();
var orders = db.Orders.Where(o => o.OrderStatus != "Готов")
.Select(o => new OrdView
{
oId = o.Id,
oData = o.Data,
oCustomersMail = customers.FirstOrDefault(c => c.CustomerId == o.CustomerId)?.Mail ?? "Не найдено",
oStartPoint = o.StartPoint,
oFinalPoint = o.FinalPoint,
oTrackNumber = o.TrackNumber,
oOrderStatus = o.OrderStatus,
oComment = o.Comment,
oMoneyPerOrder = o.MoneyPerOrder
})
.ToList();

activeOrdersGrid.ItemsSource = orders;
}
using (ProfitCalculatorDataBaseContext db = new ProfitCalculatorDataBaseContext())
{
var customers = db.Customers.ToList();
var orders = db.Orders.Where(o => o.OrderStatus != "Готов")
.Select(o => new OrdView
{
oId = o.Id,
oData = o.Data,
oCustomersMail = customers.FirstOrDefault(c => c.CustomerId == o.CustomerId)?.Mail ?? "Не найдено",
oStartPoint = o.StartPoint,
oFinalPoint = o.FinalPoint,
oTrackNumber = o.TrackNumber,
oOrderStatus = o.OrderStatus,
oComment = o.Comment,
oMoneyPerOrder = o.MoneyPerOrder
})
.ToList();

activeOrdersGrid.ItemsSource = orders;
}
8 Replies
Jimmacle
Jimmacle8mo ago
what does the error say?
ApathyErr
ApathyErrOP8mo ago
CS8072 - An expression tree lambda may not contain a null propagating operator.
Jimmacle
Jimmacle8mo ago
well that's why it's not just a lambda, it's an expression and those don't support a number of things
Jimmacle
Jimmacle8mo ago
Some C# expressions are prohibited in expression trees. Learn which...
These compiler errors and warnings indicate that an expression would include an expression that isn't allowed in an expression tree. You need to refactor your code to remove the prohibited expression.
ApathyErr
ApathyErrOP8mo ago
How else can I fix it then?
Jimmacle
Jimmacle8mo ago
by refactoring the code so the prohibited expressions aren't in the expression
JakenVeina
JakenVeina8mo ago
oCustomersMail = customers.Any()
? customers.First(c => c.CustomerId == o.CustomerId).Mail
: "Не найдено",
oCustomersMail = customers.Any()
? customers.First(c => c.CustomerId == o.CustomerId).Mail
: "Не найдено",
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server