[英]Entity Framework Many To Many Relations: Search if contains all
I've been on this problem for a while and I have no ideia how to do it. 我在这个问题上已经有一段时间了,我不知道该怎么做。 I have this Model: 我有这个模型:
public class ConversationDB
{
public int Id { get; set; }
public virtual List<UserDB> Participants { get; set; }
}
and this 和这个
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; }
}
What I'm currently struggling on is searching for a conversation that has all elements of a list of participants say: 我目前正在苦苦挣扎的是搜索包含参与者列表中所有元素的对话:
ConversationDB conversation = db.Conversations.Where(c => c.Participants.OrderBy(p => p.Username).SequenceEqual(partList.OrderBy(p => p.Username))).FirstOrDefault();
This code altough it gives me an error. 这段代码虽然给我一个错误。 What is the correct procedure I should take? 我应该采取的正确程序是什么?
EDIT: I came with this ugly and stupid solution, but is basically what I want: 编辑:我带来了这个丑陋和愚蠢的解决方案,但基本上是我想要的:
IEnumerable<ConversationDB> convs = db.Conversations.AsEnumerable();
foreach(UserDB u in partList)
{
convs = convs.Where(c => c.Participants.Contains(u));
}
Why not :) 为什么不 :)
ConversationDB conversation = db.Conversations.Where(c => c.Participants.Count() == db.Users.Count());
I think, you can't add one user twice. 我认为,您不能两次添加一个用户。
Or, more formal way: 或者,更正式的方式:
ConversationDB conversation = db.Conversations.Where(c => db.Users.All(u => c.Participants.Contains(u)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.