LINQ to SQL - Group By/Where -


i'm trying implement group messaging feature in asp.net mvc. want display list of threads specific contactid, displaying latest message in thread (no matter it's from). i've set table below:

messageid threadid messagebody contactid 10000004 300152,300160, msg1 300160 10000005 300152,300160, msg2 300160 10000008 300152,300160, msg3 300152 

i able display latest message grouped threadid. ex:

threadid count latestmessage 300152,300160, 3 10000008 

however, if add clause before group (see below), it'll filter on contactid first before doing group by, , producing result:

threadid count latestmessage 300152,300160, 2 10000005 

here's code:

        var result = s in pdc.messages                      s.contactid == contactid                      group new { s } new { s.threadid } d                      let maxmsgid = d.max(x => x.s.messageid)                      select new {                          threadid = d.key.threadid,                          count = d.count(item => item.s.messagetype == globalconstants.messagetypetext),                          lastmessage = d.where(x => x.s.messageid == maxmsgid)                      }; 

is there way group , filter on contactid?

    var result = s in pdc.messages                  s.contactid == contactid                  group new { s } new { s.threadid } d                  select new {                      threadid = d.key,                      count = d.count(item => item.s.messagetype == globalconstants.messagetypetext),                      lastmessage = d.select(x => x.s.messageid).max(),                  }; 

hope can , have nice day.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -