C#C
C#3y ago
bookuha

❔ Why .Where() doesn't work with .Include()?

I need to fetch chats that have current user as a participant, and want to fetch only active users.

I execute the following query:

var query = _db.Chats .Include(c => c.Participants.Where(cu=>cu.IsActive)) .Include(c=>c.LastMessage) .ThenInclude(m => m.Sender) .ThenInclude(cu => cu.User) .Where(c => c.Participants.Any(cu => cu.UserId == _currentUser.MessengerUserId)) .OrderByDescending(c => c.LastMessage != null ? c.LastMessage.Timestamp : c.Created) .AsNoTracking();

but participants filtering part doesn't work - all the users are included, while it works as intended with 'select only chats with current user as participant' condition.

https://i.stack.imgur.com/WDA6h.png ttps://stackoverflow.com/questions/76157093/why-where-doesnt-work-with-include - SO question
Pastebin
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Stack Overflow
I need to fetch chats that have current user as a participant, and want to fetch only active users.
I execute the following query:
var query = _db.Chats
.Include(c => c.Participants....
Was this page helpful?