加入列表集合

时间:2015-04-01 00:26:05

标签: linq join

我尝试编写一个连接LINQ查询。但它会引发语法错误。我能做什么?我需要用户的对话信息。

/*
Conversations = Header info
c.Users = List<ApplicationUser>
*/
public List<ConversationMessage> GetAll(ApplicationUser user)
{
    var query = from m in _db.ConversationMessages
        join c in _db.Conversations on m.ConversationId equals c.ConversationId
        join u in _db.Users on c.Users.Contains(user) 
        select m;
}

2 个答案:

答案 0 :(得分:1)

在我看来,它应该是更像这样的东西:

public List<ConversationMessage> GetAll(ApplicationUser user)
{
    var query =
        from c in _db.Conversations
        where c.Users.Contains(user)
        join m in _db.ConversationMessages on c.ConversationId equals m.ConversationId
        select m;

    return query.ToList();
}

答案 1 :(得分:0)

var query = from c in _db.Conversations
            where c.Users.Select(x => x.Id).Contains(user.Id)
            join m in _db.ConversationMessages on c.ConversationId equals m.ConversationId
           select m;