开发者

Select all messages but not if the subject id is the same

开发者 https://www.devze.com 2023-02-09 23:13 出处:网络
i am trying to select all the messages written from a certain from_id but only display the latest message with the same subject id. for example my messaging table looks like this:

i am trying to select all the messages written from a certain from_id but only display the latest message with the same subject id. for example my messaging table looks like this:

Id
From_ID
To_ID
Subject
Subject_ID
Date
[Content]
Percentage

each message is created with a unique ID, if the message is in the same message chain i.e. a follow on message the subject id is the same as the message ID. i have the code to select by the current user that's logged in :

Dim query = From p In db.Messages Select p Where p.From_ID = Userid

But i am unsure how 开发者_StackOverflow中文版to group it by the newest message with the same subject id. thanks in advanced.


I'm not a Visual Basic expert, but you can use Group By to group the returned messages by the SubjectID and then just select the latest message from each of the groups using Select. The syntax looks roughly like this:

Dim q = From p In db.Messaes
        Where p.From_ID = Userid
        Group By Subject = p.Subject_ID 
        Into Messages = Group
        Select ...

The ... bit needs to be replaced with a subquery that returns the latest message from messages in the current group (available in Messages).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号