我正在努力实现这里解释的内容: Creating a threaded private messaging system like facebook and gmail, 然而,我并不完全理解乔尔·布朗的答案.有没有人能解释一下.
这就是我的db表在示例数据中的样子(我假设为了演示目的我正确填写了它):
我需要显示基于LoginId的线程列表(最新在顶部),LINQ中的查询会是什么样子?(我问的是在一组消息线程中,给我每个线程中的1条最新消息)-就像在Facebook上做的那样.
I need to display ALL the messages in a message thread (LINQ) -> just like it's done on facebook where you click the message and you would see the whole "conversation" in a tread.
请帮忙!
EDIT -> continuation Joel, is this correct??
乔尔,我有点困惑,请你解释一下(粗体的 comments /问题):
这里的 idea 是,每次用户启动一个全新的线程/消息时,它都会从线程表中的一个新记录开始.然后将用户添加为ThreadParticipant,并将消息内容添加到指向包含线程的消息中.FK From Message to User(从邮件到用户的FK)表示邮件的作者.
LoginId 1 sends a message to LoginId2 => new record is inserted to MessageThread table. Also a record is inserted to MessageThreadParticipant record with MessageThreadId = 1, LoginId = 1 (the sender). And a new record is inserted into Message table with MessageId =1, MessageThreadid =1, SenderLoginId = 1 (correct??)
这是我在那次迭代之后得到的:
I think i'm confused because there is no way for Loginid 2 to know that there is a message for him. ?? OR maybe I need to insert 2 records into MessageThreadParticipant?? (the sender and the receiver)-> this way both can see the whole "conversation"??
EDIT2:个 乔,我想我能做到:
SELECT
Message.MessageId, Message.CreateDate, Message.Body, Login.Username, Message.SenderLoginId
, (SELECT MessageReadState.ReadDate
FROM MessageReadState
WHERE MessageReadState.MessageId = Message.MessageId
) as ReadDate
FROM Message
INNER JOIN Login ON Message.SenderLoginId = Login.LoginId
INNER JOIN MessageThreadParticipant mtp on mtp.MessageThreadId = Message.MessageThreadId
AND ( Message.MessageId in
( SELECT Max(Message.MessageId)
FROM MessageThreadParticipant INNER JOIN Message
ON MessageThreadParticipant.MessageThreadId = Message.MessageThreadId
GROUP BY MessageThreadParticipant.MessageThreadId
)
)
Where mtp.LoginId = 2
ORDER BY Message.CreateDate DESC;
如果我说错了,请纠正我:)