c# - 连接和分区方式中的嵌套选择
问题描述
我是 C# 的新手,我想在 Linq.plz 帮助中编写这个 TSql 代码。谢谢你
select a.Id,
a.Date,
b.Title CategoryTitle,
a.Title,
a.Description,
a.Image
from (select *,
ROW_NUMBER() over(partition by CategoryID order by Date) rankno
from News) a
join Categories b on a.CategoryID = b.Id
where rankno <= 5
解决方案
假设您有新闻和类别的枚举:
var results = News.Join(Categories, // Join News and Categories
a => a.CatergoryId,
b => b.Id,
(a,b) => new { News = a, Category = b}
)
.GroupBy(c => c.Category) // "partition by categoryId"
.SelectMany(g => g.OrderBy(gd => gd.News.CreationDate) // "order by Date"
.Take(5) // RankNo <= 5
.Select(gdd => new { // results
Id = gdd.News.Id,
Date = gdd.News.Date,
CategoryTitle = gdd.Category.Title,
Title = gdd.News.Title,
Description = gdd.News.Description,
Image = gdd.News.Image
})
);
推荐阅读
- javascript - 访问对象中的键和标签并遍历另一个数组并在循环的数组元素中分配键和标签:JavaScript
- sap-cloud-sdk - DestinationAccessException 使用 s/4 sdk
- python - 如何仅使用 SimpleImputer 或等效项转换某些列
- angular - Angular Material sidenav 始终在桌面上打开
- javascript - TypeScript - 确保数组具有字符串文字中的所有值
- selenium - ExpectedConditions.presenceOf 和 element.isPresent 有什么区别
- c# - selenium.WebDriverException:未知错误:Runtime.evaluate 在参数列表后抛出异常:SyntaxError:missing )。以前的ans没有解决
- urlencode - 呈现 Jasper 报告时出现授权错误
- android - DialogFragment - 设置状态栏颜色在某些设备上不起作用
- php - mac和windows上传文件名编码不同的问题