我试图选择使用EF和Linq的实体列表,我应该仅检索参与者ICollection中具有特定AccountId的实体
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
}
我不知道如何在LINQ中编写查询
答案 0 :(得分:3)
使用Any
:
return await _Db.Conversations
.Where(c => c.Participants.Any(p => p.Id == id))
.AsQueryable()
.ToListAsync();