简体   繁体   English

在 lambda 查询中使用“.Any()”会在 .net 核心 EF 项目中引发错误

[英]Using “.Any()” in lambda query throws error in .net core EF project

I have this below query in which throws the error below mentioned,It used to work in the other project but no able to run in the .net core project.我有这个下面的查询,其中抛出了下面提到的错误,它曾经在其他项目中工作,但无法在 .net 核心项目中运行。

var lstAppForm = await _appDBContext.ApplicationForms.Where(qr => appFormViewModel.Any(any => any.kycId == qr.id )).ToListAsync();

The LINQ expression 'DbSet.Where(a => __appFormViewModel_0.Any(any => any.kycId == a.id))' could not be translated.无法翻译 LINQ 表达式 'DbSet.Where(a => __appFormViewModel_0.Any(any => any.kycId == a.id))'。 Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估。 See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038

The problem is that the expression is not translatable to sql because the appFormViewModel is not an entity in your database.问题是该表达式不能转换为 sql 因为appFormViewModel不是数据库中的实体。 it is your view model for UI which has nothing to do directly in the database so it shouldn't be part of your EF query any way.这是您对 UI 的视图 model,它与数据库没有直接关系,因此它不应该以任何方式成为您的 EF 查询的一部分。

What we can do is project the needed data outside the EF query using Select and then use it in the Linq query for EF:我们可以做的是使用Select在 EF 查询之外投影所需的数据,然后在 EF 的 Linq 查询中使用它:

var ids = appFormViewModel.Select(x => x.kycId).ToList();

var lstAppForm = await _appDBContext.ApplicationForms
                      .Where(qr => ids.Contains(qr.id))
                      .ToListAsync();

or:或者:

var ids = appFormViewModel.Select(x => x.kycId).ToList();

var lstAppForm = await _appDBContext.ApplicationForms
                      .Where(qr => ids.Any(a => a == qr.id))
                      .ToListAsync();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM