3
A
回答
3
这应做到:
var results = table
.GroupBy(x => x.GroupId)
.Select(x => new { Row = x, Value = x.Max(y => y.Value) })
.OrderByDescending(x => x.Value)
.Select(x => x.Row)
.Take(10);
编辑:修改返回整个对象。
0
不知道这是否转换为LINQ到SQL,但这里是从L2Obj
var query = (from foo in foos
group foo by foo.GroupId into fg
select fg.OrderByDescending(f => f.Value).First())
.OrderByDescending(f => f.Value)
.Take(10);
英语的想法,这组在groupId和具有最高价值从各组选择符,如果有的话,你可以从L2SQL获得你的对象的具体列表,然后在内存中执行分组,不应该是性能/内存问题,因为你说只有100行。
对于LINQ到SQL,你可能尝试这样
var sqlQuery = (from foo in foos
join y in
(from f2 in foos
join x in
(from f1 in foos
group f1 by f1.GroupId into vg
select new { GroupId = vg.Key, MaxVal = vg.Max(f => f.Value) })
on f2.GroupId equals x.GroupId
where f2.Value == x.MaxVal
group f2 by f2.GroupId into mg
select new { GroupId = mg.Key, MinId = mg.Min(f => f.Id) })
on foo.Id equals y.MinId
orderby foo.Value descending
select foo).Take(10);
这东西是基于SQL查询来执行相同的操作
Select top 10 f.*
From Foos f
Inner Join
(Select f.GroupID, min(f.Id) as MinId
From Foos f
Inner Join
(Select GroupId, Max(Value) as MaxVal
From Foos
Group By GroupId) x
on f.GroupId = x.GroupId
and f.Value = x.MaxVal
Group By f.GroupId) y
on f.Id = y.MinId
order by f.Value desc
它基本上执行两个分组。第一个获取每个组的最大值,第二个获取每个组中具有最大值的每个记录的最小ID(如果组中的2个记录具有相同的值),然后选择前10个记录。
1
这人会得到充分的行值(它的工作对我来说与样本数据我告诉波纹管):
static void Main(string[] args)
{
Whatever one = new Whatever() {GroupId = 1, Id = 1, Value = 2};
Whatever two = new Whatever() { GroupId = 1, Id = 2, Value = 8 };
Whatever three = new Whatever() { GroupId = 2, Id = 3, Value = 16 };
Whatever four = new Whatever() { GroupId = 2, Id = 4, Value = 7 };
Whatever five = new Whatever() { GroupId = 3, Id = 5, Value = 21 };
Whatever six = new Whatever() { GroupId = 3, Id = 6, Value = 12 };
Whatever seven = new Whatever() { GroupId = 4, Id = 7, Value = 5 };
Whatever eight = new Whatever() { GroupId = 5, Id = 8, Value = 17 };
Whatever nine = new Whatever() { GroupId = 6, Id = 9, Value = 13 };
Whatever ten = new Whatever() { GroupId = 7, Id = 10, Value = 44 };
List<Whatever> list = new List<Whatever>();
list.Add(one);
list.Add(two);
list.Add(three);
list.Add(four);
list.Add(five);
list.Add(six);
list.Add(seven);
list.Add(eight);
list.Add(nine);
list.Add(ten);
var results = (from w in list
group w by w.GroupId into g
select new { GroupId = g.Key,
Value = g.Max(w => w.Value),
Id = g.OrderBy(w=>w.Value).Last().Id }).
OrderByDescending(w=>w.Value).Take(5);
foreach (var r in results)
{
Console.WriteLine("GroupId = {0},
Id = {1},
Value = {2}",
r.GroupId, r.Id, r.Value);
}
}
输出:
GroupId = 7, Id = 10, Value = 44
GroupId = 3, Id = 5, Value = 21
GroupId = 5, Id = 8, Value = 17
GroupId = 2, Id = 3, Value = 16
GroupId = 6, Id = 9, Value = 13
相关问题
- 1. LINQ的组通过,并采取计数
- 2. SQL到LINQ组通过
- 3. LINQ到SQL通过
- 4. LINQ到SQL:通过
- 5. 的LINQ采取措施,通过收集
- 6. SQL Server到LINQ组通过有
- 7. LINQ到SQL - 集团通过
- 8. 组通过LINQ到实体
- 9. LINQ到SQL:通过生成许多SQL简单组查询
- 10. 如何通过查询到asp.net mvc视图获取linq到sql组?
- 11. 如何通过声明只获取组日期的LINQ to SQL
- 12. 实体框架/ Linq to SQL:跳过并采取
- 13. LINQ,跳过并采取对抗Azure SQL数据库不工作
- 14. 的LINQ to SQL采取跳过和整体计数
- 15. 如何通过查询读取LINQ组?
- 16. 问题的LINQ跳过采取
- 17. LINQ:跳过和采取重复
- 18. LINQ的组通过组
- 19. SQL多列小组通过,以便通过对LINQ的
- 20. 通过webservice发送Linq到SQL类
- 21. LINQ到SQL搜索通过标签
- 22. Linq Group通过不采取内部实体
- 23. 如何通过Linq中使用不同的用组到SQL
- 24. 转换SQL通过和MAX与组加入到LINQ
- 25. 通过没有按转换SQL到LINQ与加盟,组由何
- 26. 有没有更好的方式,这个Linq到Sql组通过
- 27. LINQ到SQL分组
- 28. 通过LINQ to SQL中
- 29. LINQ的选择组通过
- 30. LINQ组通过问题
怎么是这种表现?这将生成一个纯粹的SQL解决方案吗?只是不想返回整个表,然后处理结果在c# – Taho 2010-09-03 01:42:47
请注意,这只返回*值*的列表,而不是全行数据。我*想*他想要整行的最高值。 – 2010-09-03 01:44:16
我确实需要整行。 – Taho 2010-09-03 01:59:05