1

我有一个包含成员的消息列表:

文本 = m.Text,用户 = m.User,日期 = m.Date

此列表包括所有消息,例如

Text = "How Are You"
User = "Michael"
Date = "1/1/12"

Text = "Well Done"
User = "Michael"
Date = "2/1/12"

Text = "Who?"
User = "John"
Date = "1/1/12"

我恳请从不会显示来自同一个人的多条消息的人那里获取最新消息,并且只提供最后一条消息,例如:

Text = "Well Done"
User = "Michael"
Date = "2/1/12"

Text = "Who?"
User = "John"
Date = "1/1/12"

如你所见; 我想消除来自同一个人的先前消息。

我目前正在使用:

var messages = (from m in mList select new
{
Text = m.Text, 
User = m.User, 
Date = m.Date
}).ToList();

我应该在这个查询中添加什么来实现我的目标?

谢谢!

4

2 回答 2

2
var messages = (from m in mList select new
{
   Text = m.Text, 
   User = m.User, 
   Date = m.Date
})
.GroupBy(m => m.User)
.Select(g => g.OrderByDescending(m => m.Date).First())
.ToList();
于 2012-08-23T07:06:27.257 回答
0
try{
    var messages = (from m in mList select new
    {
        Text = m.Text, 
        User = m.User, 
        Date = DateTime.Parse(m.Date)
    })
    .GroupBy(x => x.User).Select(x => x.First()).ToList();
}catch{}

因为日期不是字符串可排序的......

于 2012-08-23T07:08:26.317 回答