线路price = co?.price ?? 0,
下面的代码给了我上面的错误,但是如果我删除?
from co.?
效果很好。
我试图跟随这个 MSDN 示例他们在哪里使用?
在线的select new { person.FirstName, PetName = subpet?.Name ?? String.Empty };
所以,看来我需要了解何时使用?
with ??
以及什么时候不可以。
Error:
表达式树 lambda 不能包含 null 传播运算符
public class CustomerOrdersModelView
{
public string CustomerID { get; set; }
public int FY { get; set; }
public float? price { get; set; }
....
....
}
public async Task<IActionResult> ProductAnnualReport(string rpt)
{
var qry = from c in _context.Customers
join ord in _context.Orders
on c.CustomerID equals ord.CustomerID into co
from m in co.DefaultIfEmpty()
select new CustomerOrdersModelView
{
CustomerID = c.CustomerID,
FY = c.FY,
price = co?.price ?? 0,
....
....
};
....
....
}
您引用的示例使用 LINQ to Objects,其中查询中的隐式 lambda 表达式被转换为代表们...当您使用 EF 或类似的方法时,IQueryable<T>
查询,其中 lambda 表达式被转换为表达式树。表达式树不支持空条件运算符(或元组)。
只需按照旧方法进行即可:
price = co == null ? 0 : (co.price ?? 0)
(我相信空合并运算符在表达式树中很好。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)