Skip to content

Commit 604e468

Browse files
authored
feat(Table): support navigation property null value (#7296)
* fix: 修复复杂导航属性获取值表达式不支持 null 值问题 * chore: bump version 10.1.4-beta01 * refactor: 优化代码
1 parent 24729ef commit 604e468

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>10.1.3</Version>
4+
<Version>10.1.4-beta01</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Extensions/LambdaExtensions.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
@@ -662,24 +662,27 @@ Expression<Func<TModel, TResult>> GetSimplePropertyExpression()
662662
Expression<Func<TModel, TResult>> GetComplexPropertyExpression()
663663
{
664664
var propertyNames = propertyName.Split(".");
665-
Expression? body = null;
666-
Type t = type;
667-
object? propertyInstance = model;
665+
Expression body = Expression.Convert(parameter, type);
668666
foreach (var name in propertyNames)
669667
{
670-
var p = t.GetPropertyByName(name) ?? throw new InvalidOperationException($"类型 {type.Name} 未找到 {name} 属性,无法获取其值");
671-
propertyInstance = p.GetValue(propertyInstance);
672-
if (propertyInstance != null)
673-
{
674-
t = propertyInstance.GetType();
675-
}
676-
677-
body = Expression.Property(body ?? Expression.Convert(parameter, type), p);
668+
body = BuildPropertyAccess(body, body.Type, name);
678669
}
679-
return Expression.Lambda<Func<TModel, TResult>>(Expression.Convert(body!, typeof(TResult)), parameter);
670+
return Expression.Lambda<Func<TModel, TResult>>(Expression.Convert(body, typeof(TResult)), parameter);
680671
}
681672
}
682673

674+
private static ConditionalExpression BuildPropertyAccess(Expression instance, Type instanceType, string propertyName)
675+
{
676+
var p = instanceType.GetPropertyByName(propertyName) ?? throw new InvalidOperationException($"类型 {instanceType.Name} 未找到 {propertyName} 属性,无法获取其值");
677+
678+
var propertyAccess = Expression.Property(instance, p);
679+
return Expression.Condition(
680+
test: Expression.Equal(instance, Expression.Constant(null, instanceType)),
681+
ifTrue: Expression.Constant(null, p.PropertyType),
682+
ifFalse: propertyAccess
683+
);
684+
}
685+
683686
/// <summary>
684687
/// 给指定模型属性赋值 Lambda 表达式
685688
/// </summary>

0 commit comments

Comments
 (0)