我有一个EventEntity
有IEnumerable<Poc> PocEntity
。
public class EventEntity
{
public Guid Id { get; set; }
public IEnumerable<PocEntity> Poc { get; set; }
}
我试图根据EventEntity
过滤PocEntity
。所以我这样想,
IQueryable<EventEntity> query = _context.Events.Where(x => x.Poc.Where(p => p.PocId.Equals(pocId)));
但我收到错误,我无法做到这一点。请协助如何做到这一点。我正在使用EF Core。
我收到两个错误,错误1:
无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'bool'
错误2:
无法将lambda表达式转换为预期的委托类型,因为块中的某些返回类型不能隐式转换为委托返回类型
问题出在第一个条件:
.Where(x => x.Poc.Where(p => p.PocId.Equals(pocId)));
where子句需要一个bool
表达式,而它唯一得到的是一个集合:p.PocId.Equals(pocId)
解决方案:只需将Any()
添加到集合的末尾,如下所示:
.Where(x => x.Poc.Where(p => p.PocId.Equals(pocId)).Any())
该错误是因为您的第一个Where()子句的参数是错误的类型。 x => x.Poc.Where(p => p.PocId.Equals(pocId))
需要评估为bool。为此,您可以使用Any()而不是Where():
IQueryable<EventEntity> query = _context.Events.Where(x => x.Poc.Any(p => p.PocId.Equals(pocId)));