我正在尝试转换此查询,以便将其输出到自定义DTO类型对象。我想只获得我传递的int[]
版本号最高的网页。
return from page in db.Pages
where intItemIdArray.Contains(page.pageId)
group page by page.pageId into g
orderby g.Max(x => x.pageId)
select g.OrderByDescending(t => t.revision).First();
但是当我尝试替换
时select g.OrderByDescending(t => t.revision).First();
类似
select (new JPage {pageid = g.pageId, title = g.title, etc})
.OrderByDescending(t => t.revision)
.First();
它不起作用,任何人都可以帮助我吗?
这就是我目前所使用的,我不喜欢的,但它工作得很完美,而且我目前不需要进行优化。
如果有人可以改善这一点,那将会很棒。
var pages = from page in db.Pages
where intItemIdArray.Contains(page.pageId)
group page by page.pageId into g
orderby g.Max(x => x.pageId)
select g.OrderByDescending(t => t.revision).First();
return pages.Select(x => new JPage() {
pageId = x.pageId,
pageKey = x.pageKey,
title = x.title,
body = x.body,
isFolder = x.isFolder.ToString(),
leftNode = x.leftNode,
rightNode = x.rightNode,
revision = x.revision,
sort = x.sort,
createdBy = x.createdBy.ToString(),
createdDate = Utility.DateTimeToUnixTimeStamp(x.createdDate).ToString(),
modifiedDate = Utility.DateTimeToUnixTimeStamp(x.modifiedDate).ToString(),
pageVariationId = x.pagesVariationId,
parentId = x.parentId
})
.AsQueryable();
答案 0 :(得分:2)
我建议您在选择之前订购;即代替
select (new JPage {pageid = g.pageId, title = g.title, etc}
.OrderByDescending(t => t.revision).First();
你应该试试
.OrderByDescending(t => t.revision)
.Select(new JPage {pageid = g.pageId, title = g.title, etc})
.First();
如果前一个'select'
的结果中不存在,则不能按'revision'排序答案 1 :(得分:0)
这应该是一个小小的改进
var pages = from page in db.Pages
where intItemIdArray.Contains(page.pageId)
group page by page.pageId into g
select g.First(a => a.revision == g.Max(b => b.revision));