LINQ Where Exists GROUP BY



如何在LINQ中转换?

SELECT B.SENDER, B.SENDNUMBER, B.SMSTIME, B.SMSTEXT
FROM MESSAGES B
WHERE EXISTS ( SELECT A.SENDER
        FROM MESSAGES A
    WHERE A.SENDER = B.SENDER
    GROUP BY A.SENDER
    HAVING B.SMSTIME = MAX( A.SMSTIME))
GROUP BY B.SENDER, B.SENDNUMBER, B.SMSTIME, B.SMSTEXT ;

谢谢你:)

编辑! !

解决:

  var Condition = "order by SMSTime desc";      
  IEnumerable<ClassMessaggio> messaggi = Database.Select<ClassMessaggio>(Condition);; // Load all but sorted
  ElencoConversazioni =  messaggi.GroupBy(m => new { m.Number })
                .Select(g => g.OrderByDescending(m => m.SMSTime).First()).ToObservableCollection();

Try

db.Messages.Where(b => b.SmsTime == Messages.Where(a => a.Sender == b.Sender)
                                            .Max(a => a.SmsTime))

db.Messsages.GroupBy(m => new { m.Sender, m.SendNumber })
            .Select(g => g.OrderByDescending(m => m.SmsTime).First())

其中db是您的DataContext.

要显示对话列表以及"每个联系人的最后一条消息",您可以尝试如下操作:

// a query to find the last Sms time per sender
var lastSmsQuery = from m in db.messages
                 group m by m.Sender into grouping
                 select new
                 {
                     Sender = grouping.Key,
                     LastSmsTime = grouping.Max(x => x.SmsTime)
                 };
// a query to find the messages linked to the last Sms per sender
var lastMessageQuery = from m in db.messages
                       join l in lastSmsQuery on new { m.Sender, m.SmsTime } equals new { l.Sender, l.LastSmsTime }
                       select m;

使用的2-query方法与今天早些时候的问题类似-将SQL子查询转换为In到Linq Lambda

相关内容

  • 没有找到相关文章

最新更新