我有这段代码,使用带有EF
的SP从服务器获取日期 List<sp_PlacementSearchResult_Result> listResults = new List<sp_PlacementSearchResult_Result>();
var query = Context.sp_PlacementSearchResult(request.HasamaFormId, request.DepartmentId, request.InstitutionNum, request.IsSigned).AsQueryable();
这是有效的,现在我想在结果中添加一些过滤器,如下所示:
if (role.PermissionId.Equals((int)Permission.Institution))
{
query = query.Where(form => PropRole.InstitutionNum.Contains((int)form.OrganizationOrInstitutionNum));
}
当我尝试时,在函数结束时
listResults = query.ToList();
我收到错误,&#34; Nullable对象必须有值。&#34;
ToList()只有在进入if语句时才会失败。
P.S: 起初我已经使用没有SP的EF完成了这个查询:
var query = Context.APP_AuthorityHasamaForm.AsQueryable();
通过这种方式,即使进入if语句
,我也可以使用ToList()感谢名单
编辑:这是所有功能:
IEnumerable<sp_PlacementSearchResult_Result> IAuthorityHasamaFormRepository.GetByRequestFromSP(PlacementRequest request, APP_Role role, PropertiesRole PropRole)
{
List<sp_PlacementSearchResult_Result> listResults = new List<sp_PlacementSearchResult_Result>();
request.HasamaFormId = (request.HasamaFormId > 0) ? request.HasamaFormId : null;
request.InstitutionNum = (request.InstitutionNum > 0) ? request.InstitutionNum : null;
if (request.StartDate.HasValue)
{
request.StartDate = new DateTime(request.StartDate.Value.Year, request.StartDate.Value.Month, request.StartDate.Value.Day, 00, 00, 00);
}
if (request.EndDate.HasValue)
{
request.EndDate = new DateTime(request.EndDate.Value.Year, request.EndDate.Value.Month, request.EndDate.Value.Day, 00, 00, 00);
}
var query = Context.sp_PlacementSearchResult(request.HasamaFormId, request.DepartmentId, request.InstitutionNum, request.IsSigned, request.IdentityIdSign,
request.StartDate, request.EndDate, request.AuthorityNum, request.CertificateNum, null).AsQueryable();
if (role.PermissionId.Equals((int)Permission.Department))
{
query = query.Where(form => PropRole.DepartmentId.Contains(form.DepartmentId));
}
if (role.PermissionId.Equals((int)Permission.Institution))
{
query = query.Where(form => PropRole.InstitutionNum.Contains((int)form.OrganizationOrInstitutionNum));
}
try
{
listResults = query.ToList();
return listResults;
}
catch (Exception e)
{
throw;
}
listResults = query.ToList();
return listResults;
}
答案 0 :(得分:2)
您应该考虑form.OrganizationOrInstitutionNum
if (role.PermissionId.Equals((int)Permission.Institution))
{
query = query.Where(form => form.OrganizationOrInstitutionNum != null && PropRole.InstitutionNum.Contains(form.OrganizationOrInstitutionNum.Value));
}
答案 1 :(得分:0)
您没有在模式中指定哪个是可以为空的类型。然后有一些可能性。有关样本,请参阅下文:
query = query.Where(form => PropRole.InstitutionNum.Contains((int)form.OrganizationOrInstitutionNum));
如果form.OrganizationOrInstitutionNum
为空,则转换为int
将失败。试试这个:
query = query.Where(form => form.OrganizationOrInstitutionNum.HasValue && PropRole.InstitutionNum.Contains((int)form.OrganizationOrInstitutionNum.Value));
也许你可以保存来施展它。