没分了见谅啊~~求助linq的group by写法

jy02305022 2012-07-06 04:04:20
集合 list
对象 it
属性 key,message,time

要求是
根据 key 分组
然后根据 time 排序
取出最后一个对象的message

from it in list group it by it.key into g select new { g.Last().message };

这样写报错的,有没有其他方案啊?
...全文
171 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2012-07-08
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

namespace ConsoleApplication7
{
public class IT
{
public string Key { get; set; }

public string Time { get; set; }

public string Message { get; set; }
}……
[/Quote]

Entity Framework 4
风骑士之怒 2012-07-08
  • 打赏
  • 举报
回复
namespace ConsoleApplication7
{
public class IT
{
public string Key { get; set; }

public string Time { get; set; }

public string Message { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<IT> list = new List<IT>();

list.Add(new IT() { Key = "a1", Time = "1", Message = "a1" });
list.Add(new IT() { Key = "a1", Time = "2", Message = "a2" });
list.Add(new IT() { Key = "a1", Time = "3", Message = "a3" });
list.Add(new IT() { Key = "a2", Time = "4", Message = "a4" });
list.Add(new IT() { Key = "a2", Time = "5", Message = "a5" });
list.Add(new IT() { Key = "a3", Time = "7", Message = "a6" });
list.Add(new IT() { Key = "a3", Time = "6", Message = "a7" });

var q = from c in list
orderby c.Time ascending
group c by c.Key into g
select g.Last().Message;

foreach (var item in q)
{
Console.WriteLine(item);
}

Console.Read();
}
}
}
风骑士之怒 2012-07-08
  • 打赏
  • 举报
回复
5L有什么问题,,你.NET什么版本
「已注销」 2012-07-08
  • 打赏
  • 举报
回复
嗯...用循环了....
「已注销」 2012-07-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

C# code
var v = list.OrderBy(x => x.time).GroupBy(g => new { key = g.key, message = g.message, time = g.time }).Last().Key.message;
[/Quote]

是根据key分组 然后所有分组的最后一个的message
「已注销」 2012-07-06
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

C# code
var query = from l in list
orderby l.time descending
group l by l.key into g
let last = g.Last()
……
[/Quote]

报一样的错
LINQ to Entities 不识别方法“WMTClient.ChatHistory Last[ChatHistory](System.Collections.Generic.IEnumerable`1[WMTClient.ChatHistory])”,因此该方法无法转换为存储表达式。
bdmh 2012-07-06
  • 打赏
  • 举报
回复
var v = list.OrderBy(x => x.time).GroupBy(g => new { key = g.key, message = g.message, time = g.time }).Last().Key.message;
huangwenquan123 2012-07-06
  • 打赏
  • 举报
回复
            var query = from l in list
orderby l.time descending
group l by l.key into g
let last = g.Last()
select last.message;
「已注销」 2012-07-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

http://www.cnblogs.com/qiuweiguo/archive/2011/06/08/2074949.html
[/Quote]
大哥其实我不想说 我最烦你们这些就发一个链接 ,内容不对不说,可能有一些你们自己都没看过 这样有意思吗
「已注销」 2012-07-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

C# code

struct IT
{
public string key;
public string time;
public string message;
}

List<IT> list = new List<IT>();
……
[/Quote]
我说错了 是
要求是
先根据 time 排序
然后根据 key 分组
取出每组最后一个对象的message
bdmh 2012-07-06
  • 打赏
  • 举报
回复

struct IT
{
public string key;
public string time;
public string message;
}

List<IT> list = new List<IT>();
IT it = new IT();
it.key = "b";
it.time = "321";
it.message = "aaa";
list.Add(it);
it = new IT();
it.key = "a";
it.time = "123";
it.message = "bbb";
list.Add(it);
var v = list.GroupBy(g => new { key = g.key, message = g.message, time = g.time }).OrderBy(x=>x.Key.time).Last().Key.message;
MessageBox.Show(v.ToString());
mizuho_2006 2012-07-06
  • 打赏
  • 举报
回复
http://www.cnblogs.com/qiuweiguo/archive/2011/06/08/2074949.html

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧