当我尝试使用System.Linq.Dynamic.Core
库订购列表时,我得到了NullReferenceException
。
var myFinalOrderedList = myList.AsQueryable()
.OrderBy("Company.Name asc, Process.Name asc, Reference.Name asc")
.ToList();
在Reference.Name
OrderBy
上发生空异常,因为Reference
可为空。
如何按可为空的Reference
对象排序?
StackTrace(敏感信息替换为“ ****”):
{
"ClassName": "System.NullReferenceException",
"Message": "Object reference not set to an instance of an object.",
"Data": null,
"InnerException": null,
"HelpURL": null,
"StackTraceString": " at lambda_method(Closure , ControlActivity )\r\n at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count)\r\n at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count)\r\n at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count)\r\n at System.Linq.EnumerableSorter`1.ComputeMap(TElement[] elements, Int32 count)\r\n at System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count, Int32 minIdx, Int32 maxIdx)\r\n at System.Linq.OrderedEnumerable`1.GetEnumerator(Int32 minIdx, Int32 maxIdx)+MoveNext()\r\n at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n at ****.Dal.Repositories.ControlActivities.ControlActivityRepo.Search(ControlActivitySearchModel input) in C:\\Users\\****\\source\\Workspaces\\****\\Main\\****\\****.Dal\\Repositories\\ControlActivities\\ControlActivityRepo.cs:line 1189\r\n at ****.Application.ControlActivities.ControlActivityService.Search(ControlActivitySearchModel input) in C:\\Users\\****\\source\\Workspaces\\****\\Main\\****\\****.Application\\ControlActivities\\ControlActivityService.cs:line 339\r\n at ****.Api.Controllers.ControlActivityController.Search(String apikey, ControlActivitySearchModel input) in C:\\Users\\****\\source\\Workspaces\\****\\Main\\****\\****.Api\\Controllers\\ControlActivityController.cs:line 117",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": null,
"HResult": -2147467261,
"Source": "Anonymously Hosted DynamicMethods Assembly",
"WatsonBuckets": null
}
公司,流程和引用是主列表(ControlActivity)的外键。这三个几乎都具有PK ID和NVARCHAR名称。所有控制活动都需要“公司”和“过程”,并且不需要参考。
答案 0 :(得分:0)
我在@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);
}