2013-01-21 49 views
0

选择多个结果与LINQ多个字段我需要查询的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; 

感谢名单

+0

您使用的是*查询*让你的'talkerId'变量实际上是*序列*的ID。你是否期望它只有一个单一的价值? –

+0

不,我不指望它只有一个值...我想MessagePools表中有talkerId IN(1,2,3,... etc)的所有消息 –

+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; 
+0

@ user1521447:我误解了您的原始评论。现在看看。 –

+0

是的,这正是我想要的。 thanx给予宝贵的时间 –