这是一个示例表,我将在其中提取具有最高优先级的记录,对应于每对ID和代码,如下所示:
这是我达到目标的方法:
var max = from item in items
group item by new {item.code, item.id} into r
select new MyObjectType(r.Select(q => q.code),
r.Select(q => q.id),
r.Max(q => q.priority));
但结果是空的......
有什么想法解决问题吗?!
编辑:
这是一个简短的例子:
(code,id,priority)
(1,10,100)
(1,10,200)
(1,11,300)
(1,11,400)
(2,12,500)
(2,12,600)
(2,13,700)
(2,13,800)
查询的结果应为:
(1,10,200)
(1,11,400)
(2,12,600)
(2,13,800)
答案 0 :(得分:2)
在课堂上创建公共属性并执行以下操作:
var max = from item in items
group item by new {item.code, item.id} into r
select new MyObjectType
{
Code = r.Key.code,
Id = r.Key.id,
MaxValue = r.Max(q => q.priority)
};
你的课应该是这样的:
public class MyObjectType
{
public int Code { get; set; }
public int Id { get ; set; }
public int MaxValue { get; set; }
}