LINQ 存在的地方 GROUP BY

发布于 2024-10-29 12:46:32 字数 732 浏览 6 评论 0原文

我如何在 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();

How can i convert this in 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 ;

Thanks a lot :)

EDIT!!

Resolved with:

  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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青萝楚歌 2024-11-05 12:46:32

尝试

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。

Try

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

Or

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

Where db is your DataContext.

能怎样 2024-11-05 12:46:32

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

// 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

To show the list of conversations along with "the last message of every contact", you could try something like:

// 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;

The 2-query method used is similar to that of this question from earlier today - Convert SQL Sub Query to In to Linq Lambda

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文