an expression tree lambda may not contain a null propagating operator

前端 未结 4 1953
眼角桃花
眼角桃花 2020-11-28 10:49

Question: The line price = co?.price ?? 0, in the following code gives me the above error. but if I remove ? from co.?

4条回答
  •  情歌与酒
    2020-11-28 11:39

    The example you were quoting from uses LINQ to Objects, where the implicit lambda expressions in the query are converted into delegates... whereas you're using EF or similar, with IQueryable queryies, where the lambda expressions are converted into expression trees. Expression trees don't support the null conditional operator (or tuples).

    Just do it the old way:

    price = co == null ? 0 : (co.price ?? 0)
    

    (I believe the null-coalescing operator is fine in an expression tree.)

提交回复
热议问题