如何处理 Linq 表达式中的空值
How to handle null in Linq Expression
网络核心应用。我实现了通用存储库模式。我正在执行以下某种过滤器功能。
var param = Expression.Parameter(typeof(SiteAssessmentRequest), "x");
Expression<Func<SiteAssessmentRequest, bool>> query;
if(request.role == "Admin")
{
query = null;
}
else
{
query = x => x.CreatedBy == request.Userid || x.AssignedTo == request.Userid;
}
Expression body = Expression.Invoke(query, param);
if (request.Client != null && request.Client.Length != 0)
{
Expression<Func<SiteAssessmentRequest, bool>> internalQuery = x => request.Client.Contains(x.Client);
body = Expression.AndAlso(Expression.Invoke(query, param), Expression.Invoke(internalQuery, param));
}
if (request.CountryId != null && request.CountryId.Length != 0)
{
Expression<Func<SiteAssessmentRequest, bool>> internalQuery = x => request.CountryId.Contains(x.CountryId);
body = Expression.AndAlso(Expression.Invoke(query, param), Expression.Invoke(internalQuery, param));
}
var lambda = Expression.Lambda<Func<SiteAssessmentRequest, bool>>(body, param);
var siteAssessmentRequest = await _siteAssessmentRequestRepository.GetAsync(lambda, x => x.OrderByDescending(x => x.Status == Status.New.ToString()).ThenByDescending(x => x.Status == GetEnumDescription(Status.InProgress)).ThenByDescending(x => x.Status == Status.Closed.ToString()).ThenByDescending(x => x.StartDate), x => x.Country).ConfigureAwait(false);
在上面的代码中,每当管理员登录时我不想过滤。在 Admin 的情况下,当我将 null 分配给异常后的查询时,它会在下面的行
处抛出
Expression body = Expression.Invoke(query, param);
Parameter: expression is required, it cannot be empty
谁能帮我解决这个问题。任何帮助,将不胜感激。谢谢
我认为你的问题在
if(request.role == "Admin"){ query = null; }
else{
query = x => x.CreatedBy == request.Userid || x.AssignedTo == request.Userid;
}
本节。当请求来自管理员时,您不想过滤查询。所以,我想你可以试试这个
if(request.role == "Admin")
{
query = x => x.CreatedBy > 0;
}
//If CreatedBy is String Type
if(request.role == "Admin")
{
query = x => !string.IsNullOrEmpty(x.CreatedBy) && !string.IsNullOrWhiteSpace(x.CreatedBy);
}
因为,CreatedBy 应该可用于每个项目。
注意:请尝试一下,如果有效请告诉我。
网络核心应用。我实现了通用存储库模式。我正在执行以下某种过滤器功能。
var param = Expression.Parameter(typeof(SiteAssessmentRequest), "x");
Expression<Func<SiteAssessmentRequest, bool>> query;
if(request.role == "Admin")
{
query = null;
}
else
{
query = x => x.CreatedBy == request.Userid || x.AssignedTo == request.Userid;
}
Expression body = Expression.Invoke(query, param);
if (request.Client != null && request.Client.Length != 0)
{
Expression<Func<SiteAssessmentRequest, bool>> internalQuery = x => request.Client.Contains(x.Client);
body = Expression.AndAlso(Expression.Invoke(query, param), Expression.Invoke(internalQuery, param));
}
if (request.CountryId != null && request.CountryId.Length != 0)
{
Expression<Func<SiteAssessmentRequest, bool>> internalQuery = x => request.CountryId.Contains(x.CountryId);
body = Expression.AndAlso(Expression.Invoke(query, param), Expression.Invoke(internalQuery, param));
}
var lambda = Expression.Lambda<Func<SiteAssessmentRequest, bool>>(body, param);
var siteAssessmentRequest = await _siteAssessmentRequestRepository.GetAsync(lambda, x => x.OrderByDescending(x => x.Status == Status.New.ToString()).ThenByDescending(x => x.Status == GetEnumDescription(Status.InProgress)).ThenByDescending(x => x.Status == Status.Closed.ToString()).ThenByDescending(x => x.StartDate), x => x.Country).ConfigureAwait(false);
在上面的代码中,每当管理员登录时我不想过滤。在 Admin 的情况下,当我将 null 分配给异常后的查询时,它会在下面的行
处抛出Expression body = Expression.Invoke(query, param);
Parameter: expression is required, it cannot be empty
谁能帮我解决这个问题。任何帮助,将不胜感激。谢谢
我认为你的问题在
if(request.role == "Admin"){ query = null; }
else{
query = x => x.CreatedBy == request.Userid || x.AssignedTo == request.Userid;
}
本节。当请求来自管理员时,您不想过滤查询。所以,我想你可以试试这个
if(request.role == "Admin")
{
query = x => x.CreatedBy > 0;
}
//If CreatedBy is String Type
if(request.role == "Admin")
{
query = x => !string.IsNullOrEmpty(x.CreatedBy) && !string.IsNullOrWhiteSpace(x.CreatedBy);
}
因为,CreatedBy 应该可用于每个项目。
注意:请尝试一下,如果有效请告诉我。