处理LINQ语句中的null
本文关键字:null 语句 LINQ 处理 | 更新日期: 2023-09-27 18:28:20
我有一个LINQ语句,如下所示:
List<Product> products = Product.GetAll();
List<Department> departments = Deparment.GetAll();
var productList = (from product in products
select new
{
ProductId= product.ID,
ProductName = product.Name,
Departments = departments.Where(d => product.DepartmentIDs.Contains(d.Id)).ToList()
});
我的问题是,product.DepartmentIDs
字段可能为空。这是由于最近数据库发生了更改。此更改导致我的代码中断。如果product.DepartmentIDs
不为空,如何更新检索部门的Where
子句以仅获取部门?CCD_ 4将是CCD_ 5或CCD_。
departments.Where(d =>
product.DepartmentIDs != null && product.DepartmentIDs.Contains(d.Id)).ToList()