Linq Many to many in Entity Framework core

I have classes for building many to many relationships.

public class Chat 
{
    [Key]
    public Guid Id { get; set; }
    public ICollection<ApplicationUserChat> UserChats { get; set; }
}

public class ApplicationUserChat
{
[Required]
public string UserId { get; set; }
public ApplicationUser User { get; set; }

public Guid ChatId { get; set; }
public Chat Chat { get; set; }

}

public class ApplicationUser : IdentityUser
{
public ICollection<ApplicationUserChat> UserChats { get; set; }
}

For example, I have a user1 and user2

  1. I need to Select chats for user1 where chat contains a user2
  2. I need to select chat for user1 where chat contains a user2 and not contains another users (like one chat contains only 2 participants)
  3. How I can do this in EF Core?


#c-sharp #entity-framework #.net #linq

7 Likes9.30 GEEK