2016-03-15 24 views
0

我一直在这个问题上有一段时间,我没有理想的如何去做。我有这个型号:实体框架多对多关系:搜索是否包含全部

public class ConversationDB 
{ 
    public int Id { get; set; } 
    public virtual List<UserDB> Participants { get; set; } 
} 

public class UserDB 
{  
     public int Id { get; set; } 

     public String Username { get; set; } 
     public String FirstName { get; set; } 
     public String LastName { get; set; } 
     public String Email { get; set; } 
     public String Password { get; set; } 

     public virtual List<ConversationDB> Conversations { get; set; } 
} 

什么我目前正在挣扎正在寻找有与会者名单中的所有元素谈话说:

ConversationDB conversation = db.Conversations.Where(c => c.Participants.OrderBy(p => p.Username).SequenceEqual(partList.OrderBy(p => p.Username))).FirstOrDefault(); 

这段代码虽然给我一个错误。我应该采取什么正确的程序?

编辑:我想出了这个丑陋和愚蠢的解决方案,但基本上是我想:

IEnumerable<ConversationDB> convs = db.Conversations.AsEnumerable(); 
foreach(UserDB u in partList) 
{ 
     convs = convs.Where(c => c.Participants.Contains(u)); 
} 
+0

有什么错误讯息? –

+0

在System.Data.Entity.dll中发生类型'System.NotSupportedException'的异常,但未在用户代码中处理 附加信息:LINQ to Entities不识别方法'Boolean SequenceEqual [UserDB](System.Collections .Generic.IEnumerable'1 [Microphoton.Models.UserDB],System.Collections.Generic.IEnumerable'1 [Microphoton.Models.UserDB])',因此此方法不能转换为表达式。 –

+0

序列相等是一个linq对象函数 –

回答

0

为什么不:)

ConversationDB conversation = db.Conversations.Where(c => c.Participants.Count() == db.Users.Count()); 

我认为,你不能添加一个用户两次。

或者,更正式的方式:

ConversationDB conversation = db.Conversations.Where(c => db.Users.All(u => c.Participants.Contains(u)));