zeroframework/Services/DeviceCenter/ZeroFramework.DeviceCenter.Infrastructure/Specifications/Extensions/IncludeExtensions.cs
2023-12-05 17:22:48 +08:00

48 lines
1.6 KiB
C#

using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;
using ZeroFramework.DeviceCenter.Domain.Specifications;
namespace ZeroFramework.DeviceCenter.Infrastructure.Specifications.Extensions
{
public static class IncludeExtensions
{
public static IQueryable<T> Include<T>(this IQueryable<T> source, IncludeExpressionInfo info)
{
_ = info ?? throw new ArgumentNullException(nameof(info));
var queryExpr = Expression.Call(
typeof(EntityFrameworkQueryableExtensions),
"Include",
new Type[] {
info.EntityType,
info.PropertyType
},
source.Expression,
info.LambdaExpression
);
return source.Provider.CreateQuery<T>(queryExpr);
}
public static IQueryable<T> ThenInclude<T>(this IQueryable<T> source, IncludeExpressionInfo info)
{
_ = info ?? throw new ArgumentNullException(nameof(info));
_ = info.PreviousPropertyType ?? throw new NullReferenceException(nameof(info.PreviousPropertyType));
var queryExpr = Expression.Call(
typeof(EntityFrameworkQueryableExtensions),
"ThenInclude",
new Type[] {
info.EntityType,
info.PreviousPropertyType,
info.PropertyType
},
source.Expression,
info.LambdaExpression
);
return source.Provider.CreateQuery<T>(queryExpr);
}
}
}