0

我需要用 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;

谢谢

4

1 回答 1

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;
于 2013-01-21T07:12:07.873 回答