I have this code using Entity Framework
var test = _dbContext.Category
.Include(t => t.Items)
.Where(t => t.items.show == true)
.ToList();
A category has a list of items. However, I can not apply the filter show == true
.
Why is it happening? How do I fix it?
Thanks
The items in t=>t.items.show==true
is a collection not the individual item, so there is no show
property.
If you need to load only the items with show==true you can load them separately:
var categories = _dbContext.Category.ToList();
var items = _dbContext.Category.SelectMany(x => x.Items).Where(x => x.show == true).ToList();
EF will attach the items to appropriate categories automatically.
You can use Any
for this kind of request and you may avoid Include if you won't use items apart from filtering.
var test = _dbContext.Category
.Where(t => t.items.Any(item=> item.show == true))
.ToList();
PS: I strongly recommend to use C# naming convention. Items
and Show
are more accurate if they are properties.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.