关于c#:Get Max row(Linq,NHibernate)

Get Max row (Linq, NHibernate)

遇到了一个问题,我希望上次导入数据时显示失败。我最终求助于HQL,因为我必须执行两个查询。有人能理解为什么在linq to nhibernate中这不起作用(正确),这是3.1中已知的bug吗?

我要写的SQL。

1
2
3
4
5
6
7
8
9
select JobImport.* from
JobImportResult
inner join (
    select Max(JobImportResultId) as JobImportResultId
    from JobImportResult
    group by JobImportId
)as tbl on tbl.JobImportResultId = JobImportResult.JobImportResultId
inner join JobImport on JobImport.JobImportId = JobImportResult.JobImportId
where ImportFailureReasonId is not null

我最后写的是HQL。

1
2
3
select jir.JobImport from JobImportResult jir where jir.Id in
(select max(mjir.Id) from JobImportResult mjir group by mjir.JobImport)
and jir.ImportFailureReason is not null

有效的LINQ(但我想在2000行后会断开)

1
2
3
4
5
6
7
8
9
var innerQuery = Query<JobImportResult>()
    .GroupBy(jir=>jir.JobImport)
    .Select(jir=>jir.Max(jr=>jr.Id));

var innerQueryListed = innerQuery.ToList();

var resultQuery = Query<JobImportResult>()
    .Where(jir => innerQueryListed.Contains(jir.Id) && jir.ImportFailureReason != null)
    .Select(jir => jir.JobImport);

不起作用的LINQ:(

1
2
3
4
5
6
7
var innerQuery = Query<JobImportResult>()
.GroupBy(jir=>jir.JobImport)
.Select(jir=>jir.Max(jr=>jr.Id));

var resultQuery = Query<JobImportResult>()
.Where(jir => innerQuery.Contains(jir.Id) && jir.ImportFailureReason != null)
.Select(jir => jir.JobImport);


你在使用.NET 4吗?如果是,请尝试以下操作:

1
2
3
4
5
6
7
8
9
10
var innerQuery = Query<JobImportResult>()
    .GroupBy(jir=>jir.JobImport)
    .Select(jir=>jir.Max(jr=>jr.Id))
    .ToList();

var resultQuery = Query<JobImportResult>()
    .Where(jir => innerQuery.Any(j => j == jir.Id)
        && jir.ImportFailureReason != null)
    .Select(jir => jir.JobImport)
    .ToList(); //only use this if you want to resolve the query

或.NET 3.5将resultQuery部分更改为:

1
2
3
4
5
var resultQuery = Query<JobImportResult>()
    .Where(jir => innerQuery.Count(j => j == jir.Id) > 0
        && jir.ImportFailureReason != null)
    .Select(jir => jir.JobImport)
    .ToList(); //only use this if you want to resolve the query