C# 按日期排序的2个表的Linq查询
这里描述两个表 注释表C# 按日期排序的2个表的Linq查询,c#,mysql,linq,C#,Mysql,Linq,这里描述两个表 注释表 ID Comment Id OrderDate --- ---------- -------------------- 1 1 2003-10-13 08:00:00 2 2 2003-10-12 10:00:00 3 1
ID Comment Id OrderDate
--- ---------- --------------------
1 1 2003-10-13 08:00:00
2 2 2003-10-12 10:00:00
3 1 2003-10-10 12:00:00
共享表
ID Share Id OrderDate
--- ---------- --------------------
1 1 2003-10-11 08:00:00
2 2 2003-10-15 10:00:00
2 2 2003-10-12 10:00:00
现在,我想从这两个表中获取列表日期
意味着产出将
ID OrderDate
--- -------------------
3 2003-10-10 12:00:00(Comment)
1 2003-10-11 08:00:00(Share)
3 2003-10-16 12:00:00(Comment)
我们如何生成Linq查询???根据您的备注,您可以在下面的查询中尝试-
SELECT distinct id, orderdate FROM
(
SELECT id, orderdate FROM `comment`
UNION ALL
SELECT id, orderdate FROM `share`
) a
ORDER BY a.orderdate DESC
LIMIT 10;
对于如上所述设置的类,LINQ查询应该如下所示:
IEnumerable<ShareEntry> share = new List<ShareEntry>();
IEnumerable<CommentEntry> comment = new List<CommentEntry>();
var output = share
.Select(x => new
{
id = x.ID,
date = x.Date,
type = "share"
})
.Concat(comment.
Select(x => new
{
id = x.ID,
date = x.Date,
type = "comment"
}))
.OrderByDescending(x => x.date).Take(10);
IEnumerable share=new List();
IEnumerable comment=新列表();
var输出=份额
.选择(x=>new
{
id=x.id,
日期=x.日期,
type=“共享”
})
康卡特先生(评论)。
选择(x=>new
{
id=x.id,
日期=x.日期,
type=“注释”
}))
.OrderByDescending(x=>x.date).Take(10);
你能展示一些你想要的代码吗?我想把评论和分享的所有数据结合起来,并对它们进行日期排序。但我不知道如何将这些数据结合起来?因为我只想获取按日期递减的10行数据,所以请先阅读以下内容:输出中的最后一行是从哪里来的?为什么输出中只有三行?谢谢。。它起作用了。将sql查询更改为linq
IEnumerable<ShareEntry> share = new List<ShareEntry>();
IEnumerable<CommentEntry> comment = new List<CommentEntry>();
var output = share
.Select(x => new
{
id = x.ID,
date = x.Date,
type = "share"
})
.Concat(comment.
Select(x => new
{
id = x.ID,
date = x.Date,
type = "comment"
}))
.OrderByDescending(x => x.date).Take(10);