构建表达式以过滤EF Core数据

问题描述 投票:0回答:1

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

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

并为Where构建表达式:

Expression<Func<Picture, bool>> filter = w => w.EntityId > 5;

如何构建这样的表达式?

下一步操作不会在客户端执行,对吗?

var collection = _dbContext.Pictures.Where(filter).ToList();
c# entity-framework ef-core-2.2 linq-expressions
1个回答
0
投票

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

例如

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder
        .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFQuerying;Trusted_Connection=True;")
        .ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning));
}

请参见https://docs.microsoft.com/en-us/ef/core/querying/client-eval#previous-versions

© www.soinside.com 2019 - 2024. All rights reserved.