Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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_Ef Core 2.2_Linq Expressions - Fatal编程技术网

C# 构建表达式以筛选数据EF核心

C# 构建表达式以筛选数据EF核心,c#,entity-framework,ef-core-2.2,linq-expressions,C#,Entity Framework,Ef Core 2.2,Linq Expressions,我需要重用可用的表达式: Expression<Func<Picture, int>> selector = o => o.EntityId; 配置DbContext以禁止客户端评估和测试 乙二醇 看 或者开始迁移到EF Core 3.1,EF Core 3.1具有增强的查询转换功能,并已删除客户端评估功能。配置DbContext以禁止客户端评估,然后进行测试 乙二醇 看 或者开始迁移到EF Core 3.1,EF Core 3.1具有增强的查询转换功能,并删除了

我需要重用可用的表达式:

Expression<Func<Picture, int>> selector = o => o.EntityId;

配置DbContext以禁止客户端评估和测试

乙二醇


或者开始迁移到EF Core 3.1,EF Core 3.1具有增强的查询转换功能,并已删除客户端评估功能。

配置DbContext以禁止客户端评估,然后进行测试

乙二醇


或者开始迁移到EF Core 3.1,EF Core 3.1具有增强的查询转换功能,并删除了客户端评估功能。

我终于找到了动态构建表达式的方法:

Expression<Func<Picture, int>> selector = o => o.EntityId;

var parameter = Expression.Parameter(typeof(Picture));

// get property name
if (!(selector.Body is MemberExpression memberExpression))
{
    memberExpression = ((UnaryExpression)selector.Body).Operand as MemberExpression;
}
var propertyName = memberExpression.ToString().Substring(2);

var expressionParameter = Expression.Property(parameter, propertyName);
var expressionBody = Expression.GreaterThan(expressionParameter, Expression.Constant(5, typeof(int)));

var filter = Expression.Lambda<Func<Picture, bool>>(expressionBody, parameter);
var collection = _dbContext.Pictures.Where(filter).ToList();
一般示例:

var filter = CreateFilter<Picture, int>(o => o.EntityId, 5);
var collection = _dbContext.Pictures.Where(filter).ToList();

private Expression<Func<TData, bool>> CreateFilter<TData, TKey>(Expression<Func<TData, TKey>> selector, TKey valueToCompare)
{
    var parameter = Expression.Parameter(typeof(TData));
    var expressionParameter = Expression.Property(parameter, GetParameterName(selector));

    var body = Expression.GreaterThan(expressionParameter, Expression.Constant(valueToCompare, typeof(TKey)));
    return Expression.Lambda<Func<TData, bool>>(body, parameter);
}

private string GetParameterName<TData, TKey>(Expression<Func<TData, TKey>> expression)
{
    if (!(expression.Body is MemberExpression memberExpression))
    {
        memberExpression = ((UnaryExpression)expression.Body).Operand as MemberExpression;
    }

    return memberExpression.ToString().Substring(2);
}

感谢David关于的回复,我能够验证筛选没有在客户端上执行

我终于想出了如何动态构建表达式:

Expression<Func<Picture, int>> selector = o => o.EntityId;

var parameter = Expression.Parameter(typeof(Picture));

// get property name
if (!(selector.Body is MemberExpression memberExpression))
{
    memberExpression = ((UnaryExpression)selector.Body).Operand as MemberExpression;
}
var propertyName = memberExpression.ToString().Substring(2);

var expressionParameter = Expression.Property(parameter, propertyName);
var expressionBody = Expression.GreaterThan(expressionParameter, Expression.Constant(5, typeof(int)));

var filter = Expression.Lambda<Func<Picture, bool>>(expressionBody, parameter);
var collection = _dbContext.Pictures.Where(filter).ToList();
一般示例:

var filter = CreateFilter<Picture, int>(o => o.EntityId, 5);
var collection = _dbContext.Pictures.Where(filter).ToList();

private Expression<Func<TData, bool>> CreateFilter<TData, TKey>(Expression<Func<TData, TKey>> selector, TKey valueToCompare)
{
    var parameter = Expression.Parameter(typeof(TData));
    var expressionParameter = Expression.Property(parameter, GetParameterName(selector));

    var body = Expression.GreaterThan(expressionParameter, Expression.Constant(valueToCompare, typeof(TKey)));
    return Expression.Lambda<Func<TData, bool>>(body, parameter);
}

private string GetParameterName<TData, TKey>(Expression<Func<TData, TKey>> expression)
{
    if (!(expression.Body is MemberExpression memberExpression))
    {
        memberExpression = ((UnaryExpression)expression.Body).Operand as MemberExpression;
    }

    return memberExpression.ToString().Substring(2);
}

感谢David关于的回复,我能够验证筛选没有在客户机上执行

通常,是的,您希望这样做,例如,如果您有复杂的搜索查询。通常,要在编码过程中得到提示,只需将鼠标悬停在Where上:它应该是IQueryable,基本上是说查询尚未实现,而ToList的结果应该是IEnumerable,这意味着它将实现已构建的整个查询。请注意,有时,实体框架,或者更确切地说,所使用的DB SQL适配器可能不知道如何转换某些条件,但您应该得到警告,有时在执行过程中,因此请注意您的控制台!。通常,是的,您希望这样做,例如,如果您有复杂的搜索查询。通常,要在编码过程中得到提示,只需将鼠标悬停在Where上:它应该是IQueryable,基本上是说查询尚未实现,而ToList的结果应该是IEnumerable,这意味着它将实现已构建的整个查询。请注意,有时,实体框架,或者更确切地说,所使用的DB SQL适配器可能不知道如何转换某些条件,但您应该得到警告,有时在执行过程中,因此请注意您的控制台!。谢谢,我还不知道这种可能性。它工作并抛出一个非常有用的异常。例如:为警告“Microsoft.EntityFrameworkCore.Query.QueryClientEvaluationWarning:LINQ表达式”where[w].EntityId.ToString.StartsWith6“生成的错误无法转换,将在本地计算。”。通过将事件ID“RelationalEventId.QueryClientEvaluationWarning”传递给“DbContext.OnConfiguring”或“AddDbContext”中的“ConfigureWarnings”方法,可以抑制或记录此异常。谢谢,我不知道这种可能性。它工作并抛出一个非常有用的异常。例如:为警告“Microsoft.EntityFrameworkCore.Query.QueryClientEvaluationWarning:LINQ表达式”where[w].EntityId.ToString.StartsWith6“生成的错误无法转换,将在本地计算。”。通过将事件ID“RelationalEventId.QueryClientEvaluationWarning”传递给“DbContext.OnConfiguring”或“AddDbContext”中的“ConfigureWarnings”方法,可以抑制或记录此异常。
var filter = CreateFilter<Picture, int>(o => o.EntityId, 5);
var collection = _dbContext.Pictures.Where(filter).ToList();

private Expression<Func<TData, bool>> CreateFilter<TData, TKey>(Expression<Func<TData, TKey>> selector, TKey valueToCompare)
{
    var parameter = Expression.Parameter(typeof(TData));
    var expressionParameter = Expression.Property(parameter, GetParameterName(selector));

    var body = Expression.GreaterThan(expressionParameter, Expression.Constant(valueToCompare, typeof(TKey)));
    return Expression.Lambda<Func<TData, bool>>(body, parameter);
}

private string GetParameterName<TData, TKey>(Expression<Func<TData, TKey>> expression)
{
    if (!(expression.Body is MemberExpression memberExpression))
    {
        memberExpression = ((UnaryExpression)expression.Body).Operand as MemberExpression;
    }

    return memberExpression.ToString().Substring(2);
}