EF LINQ selects objects where id is in the list

I was trying to select a list of objects using EF and Linq and I should only get those that have a specific AccountId inside the ICollection of members.

public class Conversation
{
    public Conversation()
    {
        Participants = new List<Account>();
        Messages = new List<Message>();
    }

    [Key]
    public Int32 conversationId { get; set; }
    public ICollection<Message> Messages { get; set; }
    public ICollection<Account> Participants { get; set; }
}

      

_Db - DbContext

public async Task<List<Conversation>> FindByAccountIdAsync(Int32 id)
{
   return await _Db.Conversations.Where(....).ToListAsync(); 
   // .... select conversations where id == AccountId inside ICollection<Account> Participants
}

      

I don't know how to compose a query in LINQ

+3


source to share


1 answer


Use Any

:



return await _Db.Conversations
    .Where(c => c.Participants.Any(p => p.Id == id))
    .AsQueryable()
    .ToListAsync();

      

+3


source







All Articles