0

I need to query a table with linq and bind the rows returned from the linq query to an object

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;

thanx

4
  • You're using a query so your talkerId variable is actually a sequence of IDs. Are you expecting it to only have a single value?
    – Jon Skeet
    Jan 21, 2013 at 6:49
  • no I am not expecting it to only have a single value... I want all message in MessagePools tables which has talkerId IN(1,2,3,...etc) Jan 21, 2013 at 7:05
  • Then you need to put that in the code...
    – Jon Skeet
    Jan 21, 2013 at 7:07
  • Gah - misread the comment. Editing...
    – Jon Skeet
    Jan 21, 2013 at 7:21

1 Answer 1

1

It sounds like you want a join. If you don't need talkerId for anything else, it's going to be simplest to do this in a single query:

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;
2
  • @user1521447: I misread your original comment. Have a look now.
    – Jon Skeet
    Jan 21, 2013 at 7:23
  • yes that is exactly i want. thanx for giving for valuable time Jan 21, 2013 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.