Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何实现EF核心定制规范中的包含?_C#_Entity Framework Core - Fatal编程技术网

C# 如何实现EF核心定制规范中的包含?

C# 如何实现EF核心定制规范中的包含?,c#,entity-framework-core,C#,Entity Framework Core,EF核心3.1 我已经看到了这个示例,并希望实现包含模式 public static class QuerySpecificationExtensions { public static IQueryable<T> Specify<T>(this IQueryable<T> query, ISpecification<T> spec) where T : class { // fetch a Queryable th

EF核心3.1 我已经看到了这个示例,并希望实现包含模式

public static class QuerySpecificationExtensions
{
    public static IQueryable<T> Specify<T>(this IQueryable<T> query, ISpecification<T> spec) where T : class
    {
        // fetch a Queryable that includes all expression-based includes
        var queryableResultWithIncludes = spec.Includes
            .Aggregate(query,
                (current, include) => current.Include(include));

        // modify the IQueryable to include any string-based include statements
        var secondaryResult = spec.IncludeStrings
            .Aggregate(queryableResultWithIncludes,
                (current, include) => current.Include(include));

        // return the result of the query using the specification's criteria expression
        return secondaryResult.Where(spec.Criteria);
    }
}
公共静态类QuerySpecificationExtensions
{
公共静态IQueryable指定(此IQueryable查询,isSpecification规范),其中T:class
{
//获取包含所有基于表达式的包含的可查询项
var queryableResultWithIncludes=spec.Includes
.合计(查询,
(当前,包括)=>当前。包括(包括));
//修改IQueryable以包含任何基于字符串的include语句
var secondaryResult=规格IncludeStrings
.骨料(查询结果),包括:,
(当前,包括)=>当前。包括(包括));
//使用规范的条件表达式返回查询结果
返回secondaryResult.Where(规格标准);
}
}
我可以将其添加到字符串中,例如“User.UserRole.Role”,但我想实现对象。
可能不可能吗?

包括上述
i规范的成员
声明为

List<Expression<Func<T, object>>> Includes { get; }
为具有
User
属性且具有
UserRoles
集合且具有
Role
属性的实体添加示例如下:

Includes.Add(q => q.Include(e => e.User).ThenInclude(e => e.UserRoles).ThenInclude(e => e.Role));
Specify
方法实现的相应部分将是:

var queryableResultWithIncludes = spec.Includes
    .Aggregate(query,
        (current, include) => include(current));
var queryableResultWithIncludes = spec.Includes
    .Aggregate(query,
        (current, include) => include(current));