通过上次查询以多个结果选择LINQ多个字段

时间:2013-01-21 06:44:20

标签: linq-to-sql

我需要使用linq查询表并将从linq查询返回的行绑定到对象

WebChatDBDataContext dataContext = new WebChatDBDataContext();
var executiveSession= dataContext.ExecutiveSessions.FirstOrDefault(s => s.SessionId == httpcontext.Session.SessionID);
var talkerId = (from cRoom in dataContext.ChatRooms
                where cRoom.ExecutiveId == executiveSession.ExecutiveSessionId
                select cRoom.TalkerId
               );
var msglst = from msgPool in dataContext.MessagePools
             // I want to use talkerId from the previous query
             where msgPool.TalkerId == ???
            select msglst;

感谢名单

1 个答案:

答案 0 :(得分:1)

听起来你想要加入。如果您不需要其他任何内容talkerId,那么在单个查询中执行此操作最简单:

var executiveSessionId = dataContext.ExecutiveSessions
        .FirstOrDefault(s => s.SessionId == httpcontext.Session.SessionID)
        .ExecutiveSessionId;

var pools = from room in dataContext.Rooms
            where room.ExecutiveId == executiveSessionId
            join pool in dataContext.MessagePools
              on room.TalkerId equals pool.TalkerId
            select pool;