为什么这个表达式在 ef core 3.1 c# 解析时会失败?

Why does this expression fail when parsed by ef core 3.1 c#?

我正在尝试使用此表达式 (dbIdGetter):

f => f.FormTemplateId

并在 "Contains" 表达式中使用它:

IEnumerable<ApiEntity> batch = apiEntities.Take(batchSize);
IEnumerable<IdType> batchIds = batch.Select(apiIdGetterCompiled).ToList();

var parameter = Expression.Parameter(typeof(DbEntity), "f");

var batchIdsConst = Expression.Constant(batchIds);
var containsMethod = typeof(List<IdType>).GetMethods().FirstOrDefault(y => y.Name == "Contains");
var body = Expression.Call(Expression.Constant(batchIds), containsMethod, dbIdGetter.Body);

var expr = Expression.Lambda<Func<DbEntity, bool>>(
    body, 
    parameter
);

// Retrieve all the ApiEntity that already exist inside the db to update
IEnumerable<DbEntity> dbBatch = await dbSet.Where(expr).ToListAsync();

"expr" 的调试字符串为:

{f => value(System.Collections.Generic.List`1[System.Int32]).Contains(f.FormTemplateId)}

这次通话:

IEnumerable<DbEntity> dbBatch = await dbSet.Where(expr).ToListAsync();

错误:

System.InvalidOperationException: 'The LINQ expression 'DbSet<SomeClass>
.Where(f => List<int> { 2406369, 2487609, 1997152, 1781014, 2239271, 2256035, 2256334, 2360990, 2256541, 2256652, 2360051, 2256568, 2256600, 2256482, 2256057, 2256088, 2239292, 1934309, }.Contains(f.FormTemplateId))' 

我做错了什么?

谢谢 Ivan Stoev,他的评论为我解决了问题:

Using lambda body with new parameter looks suspicious. Worth trying var parameter = dbIdGetter.Parameters[0];

我变了

var parameter = Expression.Parameter(typeof(DbEntity), "f");

var parameter = dbIdGetters.Parameters[0]