1 回答
TA贡献1893条经验 获得超10个赞
在@Alen 和https://github.com/StefH/System.Linq.Dynamic.Core/issues/98发布的链接的帮助下,我找到了自己问题的答案。下面,我将展示更多我的实际代码,这就是它看起来与问题不同的原因。答案在于StaticMethod.ConvertToNullableNested. 例如,我正在传递,{i.ColumnName} {i.SortOrder} = "Company.Name asc"它正在返回Company == null ? null : Company.Name asc。然后我将每个排序附加在一起并传递给 OrderBy。
//TURN INTO QUERYABLE
var finalList = unionList.AsQueryable();
//DYNAMIC SORT
if (input.SortModel?.SortModelItems?.Count > 0)
{
string sortQry = String.Empty;
foreach (var i in input.SortModel.SortModelItems)
{
sortQry = sortQry + $"{StaticMethod.ConvertToNullableNested($"{i.ColumnName} {i.SortOrder}")}, ";
}
sortQry = sortQry.TrimEnd(", ");
finalList = finalList.OrderBy(sortQry);
}
//RETURN AND REMEMBER TO .TAKE(Pagesize)
return Tuple.Create(finalList.Take(input.PageSize).ToList(), finalRowCount);
public static string ConvertToNullableNested(string expression, string result = "", int index = 0)
{
//Transforms => "a.b.c" to "(a != null ? (a.b != null ? a.b.c : null) : null)"
if (string.IsNullOrEmpty(expression))
return null;
if (string.IsNullOrEmpty(result))
result = expression;
var properties = expression.Split(".");
if (properties.Length == 0 || properties.Length - 1 == index)
return result;
var property = string.Join(".", properties.Take(index + 1));
if (string.IsNullOrEmpty(property))
return result;
result = result.Replace(expression, $"{property} == null ? null : {expression}");
return ConvertToNullableNested(expression, result, index + 1);
}
- 1 回答
- 0 关注
- 176 浏览
添加回答
举报