[英]Obtaining entities from DbSet from a list of matching objects
I'm using Entity Framework Core 6
and I want to find a series of entities in a DbSet
.我正在使用
Entity Framework Core 6
,我想在DbSet
中找到一系列实体。 The entities I want to obtain are the ones match some properties in a list of input objects.我要获取的实体是与输入对象列表中的某些属性匹配的实体。
I've tried something like this:我试过这样的事情:
public IEnumerable<MyEntity> FindEntities(IEnumerable<MyEntityDtos> entries)
{
return dbContext.MyDbSet.Where(r => entries.Any(e => e.Prop1 == r.Prop1 && e.Prop2 == r.Prop2));
}
But I get the classic EF Core exception saying that my LINQ cannot be translated to a database query (the problem in particular is the entries.Any(...)
instruction)但我得到经典的 EF Core 异常,说我的 LINQ 无法转换为数据库查询(问题尤其是
entries.Any(...)
指令)
I know I can just loop over the list of entries
and obtain the entities one by one from the DbSet, but that is very slow, I was wondering if there was a more efficient way to do this in EF Core that I don't know about.我知道我可以遍历
entries
列表并从 DbSet 中一一获取实体,但这非常慢,我想知道在我不知道的 EF Core 中是否有更有效的方法来执行此操作关于。
I think this should work:我认为这应该有效:
public IEnumerable<MyEntity> FindEntities(IEnumerable<MyEntityDtos> entries)
{
var props1=entries.Select(x=>x.Prop1).ToArray();
var props2=entries.Select(x=>x.Prop2).ToArray();
return dbContext.MyDbSet.Where(r => props1.Contains(r.Prop1) && props2.Contains(r.Prop2));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.