我在我的表tbl_JobMaster
中有以下记录ID CIN JobNumber Version
1 ABC 100 1.0
2 ABC 100 2.0
3 ABC 200 1.0
4 ABC 200 2.0
5 ABC 200 3.0
6 XYZ 300 1.0
我想要基于CIN列和MAX(版本)以及唯一JobNumber
的记录列表例如CIN = ABC 所以输出应该如下
ID CIN JobNumber Version
2 ABC 100 2.0
5 ABC 200 3.0
我尝试了以下代码,但它没有工作
var result=(from job in entity.tbl_JobMaster
where job.CIN=="ABC" && job.Version==entity.tbl_JobMaster.Where(p=>p.ID==job.ID).Max(p=>p.Version)
select job).Distinct();
答案 0 :(得分:1)
以下是使用LINQ扩展方法的示例:
var selection = entity.tbl_JobMaster
.Where(job => job.CIN == "ABC")
.GroupBy(job => job.JobNumber)
.Select(group => group
.OrderByDescending(job => job.Version)
.First()
);
答案 1 :(得分:0)
您的查询应如下所示
var result = (from job in entity.tbl_JobMaster
where job.CIN == "ABC" && job.Version == entity.tbl_JobMaster.Where(p => p.CIN == job.CIN && p.JobNumber == job.JobNumber).Max(p => p.Version)
select job).Distinct();
答案 2 :(得分:0)
第一组由CIN和JobNumber
组成var temp = from job in entity.tbl_JobMaster
where job.CIN == "ABC"
group job by new { job.CIN, job.JobNumber } into g
select g;
然后按版本降序对组中的项目进行排序,并选择第一个
var result = temp.Select(o => o.OrderByDescending(t => t.Version).First());