从多个列中选择最新数据 [英] select most recent data from multiple colums
本文介绍了从多个列中选择最新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试选择数据库中的30个最新条目,但我想从4个不同的表中选择它们。
有没有办法用linq干净快速地做到这一点?
在查询结束时,我想将这些对象存储在列表中。
谢谢提前!
解决方案
这样的事情?
var items =
(来自atable中的_tableOne
加入b中a.ID上的_tableTwo中的b等于b.A_ID
a.ID上的_tableThree中的c等于c.A_ID
加入a _tableFour中的a。 ID等于d.A_ID
orderby a.ID降序
选择新的{a.Name,b.B_Field,c.C_Field d.D_Field})。Take(30)
这样的订购日期对我有用。
您的日期是什么格式?你可能会得到不同的结果。
你的结果是什么顺序?
var feedList =
((来自 video 在 context.Video
orderby video.Date 降序
< span class =code-sdkkeyword> select new {TypeID = video.VideoID,CodeID = video.CodeID,Date = video.Date})。 Take( 50 ))
.Union(( from comment in context.Comment
orderby comment.Date descending
选择 new {TypeID = comment.CommentID,CodeID = comment.CodeID,Date = comment.Date})。Take(< span class =code-digit> 50 ))
.Union(( from share 在 context.Share
orderby share.DateTime 降序
< span class =code-sdkkeyword> select new {TypeID = share.ShareID,CodeID =( int ?)share.CodeID,Date = share.DateTime})。Take( 50 ))
.Union((来自事件 context.Event
orderby 事件。 CreateDate
选择 new {TypeID = events.EventID,CodeID = events.CodeID,Date = events.CreateDate}) .Take( 50 ))
.ToList();
foreach ( var f feedList中的class =code-keyword>
{
var feed = new FeedListItemsContract();
{
feed.TypeId = f.TypeID;
var blCode = new BLCode();
feed.CodeName = blCode.GetCodeNameById(( int )f.CodeID);
feed.Date = f.Date;
}
result.Add(feed);
}
var resultSorted = result.OrderByDescending(s = > s.Date)。ToList();
return resultSorted;
这就是我的意思需要做的才能使它工作..
重要的是每个选择返回相同数量的表格!
感谢您的帮助!
Hi,
I''m trying to select the 30 latest entries in my database, but I want to select them from 4 different tables.
Is there a way to do this clean and quick using linq?
At the end of the query I want to store these objects in a list.
Thanks in advance!
解决方案
Something like this?
var items = (from a in _tableOne join b in _tableTwo on a.ID equals b.A_ID join c in _tableThree on a.ID equals c.A_ID join d in _tableFour on a.ID equals d.A_ID orderby a.ID descending select new { a.Name, b.B_Field, c.C_Field d.D_Field}).Take(30)
Ordering dates like that works for me.
What format are your dates in? You might get different results depending on that.
What order are your results in?
var feedList = ((from video in context.Video orderby video.Date descending select new { TypeID = video.VideoID, CodeID = video.CodeID, Date = video.Date }).Take(50)) .Union((from comment in context.Comment orderby comment.Date descending select new { TypeID = comment.CommentID, CodeID = comment.CodeID, Date = comment.Date }).Take(50)) .Union((from share in context.Share orderby share.DateTime descending select new { TypeID = share.ShareID, CodeID = (int?)share.CodeID, Date = share.DateTime }).Take(50)) .Union((from events in context.Event orderby events.CreateDate select new { TypeID = events.EventID, CodeID = events.CodeID, Date = events.CreateDate }).Take(50)) .ToList(); foreach (var f in feedList) { var feed = new FeedListItemsContract(); { feed.TypeId = f.TypeID; var blCode = new BLCode(); feed.CodeName = blCode.GetCodeNameById((int)f.CodeID); feed.Date = f.Date; } result.Add(feed); } var resultSorted = result.OrderByDescending(s => s.Date).ToList(); return resultSorted;
that''s what I needed to do to make it work..
important is that every select returns the same ammount of tables!
thanks for the help!
这篇关于从多个列中选择最新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文