我有以下动态LINQ运行没有错误。net Core 3.1
query = query.GroupJoin(ParsingConfig.DefaultEFCore21,
detailObject,
"new { it.ExternalDataId2.Value AS I0}",
"new { Id AS I0}",
"new (outer AS A, inner AS B)");
query = query.SelectMany("B.DefaultIfEmpty()",
"new { source.A.Id AS Id,source.A.ExternalDataId2 AS ExternalDataId2,detail.Title AS ExternalData2Ref_Title,detail.Id AS ExternalData2Ref_Id}",
"source",
"detail");
query.ToDynamicListAsync();
我正在将我的应用程序移植到。net 6.0,并有这个错误:空对象必须有一个值。
我试着删除"ParsingConfig"没有解决问题。使用SQL Profiler,我看到查询被执行.
完整异常堆栈跟踪:
Message:
Test method FrameworkTests.DataLayer.FindTests.FindValueExternal threw exception:
NTSFramework.DataLayer.DataLayerException: Errore generico di repository Nullable object must have a value.
---> System.InvalidOperationException: Nullable object must have a value.
Stack Trace:
lambda_method559(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
AsyncEnumerator.MoveNextAsync()
DynamicEnumerableAsyncExtensions.ToListAsync[T](IEnumerable source, CancellationToken cancellationToken)
DynamicEnumerableAsyncExtensions.ToListAsync[T](IEnumerable source, CancellationToken cancellationToken)
BaseDataLayer.FindValuesAsync[TModel](FindValuesOptions findOptions) line 866
如下所示更改SelectMany不再产生错误,但这不是我需要的结果:
query = query.SelectMany("B.DefaultIfEmpty()",
"new { source.A.Id AS Id,source.A.ExternalDataId2 AS ExternalDataId2,detail As ExternalData2Ref}",
"source",
"detail");
问题来自您的SelectMany
表达式中的B.DefaultIfEmpty()
。如果连接没有产生任何b,则detail
实体为null
,并且不能取消对detail.Title
或detail.Id
的引用。
你可以显式地检查null,而不是只写detail.Title
,你必须写detail != null ? detail.Title : string.Empty AS ExternalData2Ref_Title
。或者您可以使用动态LINQ空传播函数np()
并写入np(detail.Title)
。
在你的代码中,它应该看起来像这样(我假设detail.Id
的类型是int
):
query = query.SelectMany("B.DefaultIfEmpty()",
"new { source.A.Id AS Id,source.A.ExternalDataId2 AS ExternalDataId2, np(detail.Title) AS ExternalData2Ref_Title, detail != null ? detail.Id : 0 AS ExternalData2Ref_Id}",
"source",
"detail");