EF LINQ选择Id在列表中的实体
我试图使用EF和Linq选择一个实体列表,我应该只检索那些在参与者ICollection中具有特定AccountId的实体EF LINQ选择Id在列表中的实体,linq,entity-framework,Linq,Entity Framework,我试图使用EF和Linq选择一个实体列表,我应该只检索那些在参与者ICollection中具有特定AccountId的实体 public class Conversation { public Conversation() { Participants = new List<Account>(); Messages = new List<Message>(); } [Key] public Int3
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; }
}
公共课堂对话
{
公开对话
{
参与者=新列表();
消息=新列表();
}
[关键]
公共Int32会话ID{get;set;}
公共ICollection消息{get;set;}
公共ICollection参与者{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
}
公共异步任务findbyaccountadasync(Int32 id)
{
return wait _Db.Conversations.Where(..).tolistSync();
//..选择对话,其中id==ICollection参与者内的AccountId
}
我不知道如何在LINQ中组合查询使用:
谢谢David,我收到一个错误,上面写着:System.Func)“”不能从用法中推断出来。尝试显式指定类型参数。现在显然太晚了,忽略前面的想法,改用这个,更简单。谢谢David,我仍然收到一个错误“System.Linq.IQueryable”不包含“ToListSync”的定义,并且没有扩展方法“ToListSync”接受类型为“System.Linq.IQueryable”的第一个参数。我想您应该像Participants=IEnumerable()那样更改列表;或IQueryable可用于ToListSync()@Gavello添加了一个额外的行,使您能够执行
ToListSync
return await _Db.Conversations
.Where(c => c.Participants.Any(p => p.Id == id))
.AsQueryable()
.ToListAsync();