C# 如何使用Max函数在多个列上使用group by
下面是一个示例表,我将在其中提取与每对ID和代码对应的具有最高优先级的记录,如下所示: 以下是我实现目标的方法:C# 如何使用Max函数在多个列上使用group by,c#,linq,linq-to-sql,C#,Linq,Linq To Sql,下面是一个示例表,我将在其中提取与每对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 =&g
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)
在类中公开属性并执行以下操作:
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; }
}
在类中公开属性并执行以下操作:
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; }
}
在类中公开属性并执行以下操作:
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; }
}
在类中公开属性并执行以下操作:
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; }
}
你能告诉我这些数据是什么意思吗collection@EhsanSajjad当前位置请检查update@mainking检查我的帖子会有帮助的你能告诉我这些数据是什么意思吗collection@EhsanSajjad当前位置请检查update@mainking检查我的帖子会有帮助的你能告诉我这些数据是什么意思吗collection@EhsanSajjad当前位置请检查update@main国王查看我的帖子会有帮助的。你能告诉我这些数据是什么意思吗collection@EhsanSajjad当前位置请检查update@main国王检查我的帖子会有帮助的那是关心,谢谢你。。。你是说空输出的唯一原因是缺少对类的对象字段的引用?!。。。您在构造函数中传递了它们,但并没有向类实现展示您是如何做的,但通常使用这种方式这是需要注意的,谢谢。。。你是说空输出的唯一原因是缺少对类的对象字段的引用?!。。。您在构造函数中传递了它们,但并没有向类实现展示您是如何做的,但通常使用这种方式这是需要注意的,谢谢。。。你是说空输出的唯一原因是缺少对类的对象字段的引用?!。。。您在构造函数中传递了它们,但并没有向类实现展示您是如何做的,但通常使用这种方式这是需要注意的,谢谢。。。你是说空输出的唯一原因是缺少对类的对象字段的引用?!。。。您在构造函数中传递它们,但并没有向类实现展示您是如何做的,但通常使用这种方式