我有列表包含需要与数据库进行比较的所有值
from staff in this.unitOfWork.StaffRepository.Data
where officeIds.Contains(staff.OfficeId)
officeIds具有int []类型,officeIds可以为null。如果officeIds为null,我该怎么办?
我试过
where officeIds != null && officeIds.Contains(staff.OfficeId)
但它不起作用。它会引发异常
类型' System.NotSupportedException'的例外情况发生在EntityFramework.SqlServer.dll中但未在用户代码中处理
附加信息:无法比较类型的元素 ' System.Int32 []&#39 ;.只有原始类型,枚举类型和实体 支持类型。
答案 0 :(得分:0)
感谢haim770的建议,以下是他的评论:
officeIds数组就在那里,以便Linq to Entities能够发出一个SQL IN(...,...)运算符,检查它的无效性不是提供者应该做的事情。
你最好总是将officeIds初始化为一个空数组(officeIds = new int [])或者在检查无效之后附加where子句(后一种方法听起来对我来说更合理)
这里我们应该开箱即思考而不是关注如何在where
语句中检查null。
问题陈述:
from staff in this.unitOfWork.StaffRepository.Data
where officeIds.Contains(staff.OfficeId)
以下in
接受IQueryable
。我们可以从from
var query = this.unitOfWork.StaffRepository.Data
if(list != null && list.Count > 0)
query = query.Where("condition")
from staff in query
where *continue other conditions*
select ...