我有一组办公室 ID,并且该数组可能为空。如果 officeIdsToSelect 数组为空,我希望 EF 查询返回所有记录,或者如果它不为空,则只返回匹配的记录。然而这:
int[] officeIdsToSelect = new int[] { 1, 2, 3 };
Office[] selectedOffices = (from item in new TdsDb().Offices
where (officeIdsToSelect == null || officeIdsToSelect.Contains(item.OfficeID))
select item).ToArray();
抛出异常:
System.NotSupportedException : Cannot compare elements of type 'System.Int32[]'. Only primitive types (such as Int32, String, and Guid) and entity types are supported.
具体来说,Linq to Entities 反对officeIdsToSelect == null
. 我明白它在说什么(更清晰的 EF 错误消息之一......)
那么我怎样才能在这里得到我想要的呢?