How to filter a List record within another list c#

Asked

Viewed 41 times

0

I have a class of App where in that class contains a ICollection<Comentarios>

Now I need to filter ALL comments who have Status == Aprovado, where this Status is an Enum.

I tried to do it this way:

IEnumerable<App> app = await GetAsync(
    c => c.Comments.Where(d => d.Status == Status.Approved)
);
public async Task<IEnumerable<T>> GetAsync(Expression<Func<T, bool>> predicate) 
{ 
    return await _dbContext.Set<T>().Where(predicate).ToListAsync(); 
}

the error is returned:

Cannot implicitly Convert type 'System.Collections.Generic.Ienumerable<Showcase.Core.Entities.Comment>' to 'bool'

  • How is that method? GetAsync

  • @Marconi public async Task<Ienumerable<T>> Getasync(Expression<Func<T, bool>> predicate) { Return await _dbContext.Set<T>(). Where(predicate). Tolistasync(); }

  • Only await GetAsync(d => d.Status == Status.Approved); doesn’t work?

1 answer

0


As you are using EF Core 5, you can make a Include with filter. See more in this post about the news of EF Core 5.

var apps = _dbContext.Set<App>()
               .Include(a => a.Comentarios.Where(c => c.Status == Status.Approved));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.