[英]Filter Stored Procedure Results using Linq
I have this code that get date from server using SP with EF 我有这段代码可以从使用SP和EF的服务器获取日期
List<sp_PlacementSearchResult_Result> listResults = new List<sp_PlacementSearchResult_Result>();
var query = Context.sp_PlacementSearchResult(request.HasamaFormId, request.DepartmentId, request.InstitutionNum, request.IsSigned).AsQueryable();
This is working, now i'd like to add some filter to the result like this: 这正在工作,现在我想向结果添加一些过滤器,如下所示:
if (role.PermissionId.Equals((int)Permission.Institution))
{
query = query.Where(form => PropRole.InstitutionNum.Contains((int)form.OrganizationOrInstitutionNum));
}
at the end of the function when i try to do 在功能结束时,我尝试执行
listResults = query.ToList();
I get error, "Nullable object must have a value." 我收到错误消息:“可为空的对象必须具有一个值。”
The ToList() fail only if it's get into the if statement. ToList()仅在进入if语句时失败。
PS: At first i have done this query with EF without SP like this: PS:最初,我是使用EF而没有SP的情况下执行以下查询:
var query = Context.APP_AuthorityHasamaForm.AsQueryable();
And in that way i have no problem doing the ToList() even if it get into the if statement 这样,即使它进入了if语句,我也不会做ToList()
Thanx 感谢名单
Edit: this is all the function: 编辑:这是所有功能:
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;
}
You should consider possible null values of form.OrganizationOrInstitutionNum
您应该考虑form.OrganizationOrInstitutionNum
可能的空值
if (role.PermissionId.Equals((int)Permission.Institution))
{
query = query.Where(form => form.OrganizationOrInstitutionNum != null && PropRole.InstitutionNum.Contains(form.OrganizationOrInstitutionNum.Value));
}
You do not specify which is a nullable type on your mode. 您没有在模式中指定哪个是可空类型。 Then there are some possibilities. 然后有一些可能性。 For sample, the statment bellow: 例如,下面的陈述:
query = query.Where(form => PropRole.InstitutionNum.Contains((int)form.OrganizationOrInstitutionNum));
if form.OrganizationOrInstitutionNum
is null, the cast to int
will fail. 如果form.OrganizationOrInstitutionNum
为null,则转换为int
将失败。 Try this: 尝试这个:
query = query.Where(form => form.OrganizationOrInstitutionNum.HasValue && PropRole.InstitutionNum.Contains((int)form.OrganizationOrInstitutionNum.Value));
Maybe you can be save to cast it. 也许您可以保存以进行投射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.