This is most likely new to the LINQ question, but provided that I have a set of elements with the DateTime property, with one date having at most one element, how can I select the N most recent elements from the link date, i.e. N elements whose date is less than the requested date and the largest date?My naive thought would be to first select the elements with a date less than the key date, sort by date and select the N first elements from this subset.
var recentItems = from item in dataContext.Items where item.Date<=date orderby item.Date descending select item; var mostRecentItems = recentItems.Take(5).ToList();
Is this the “right” way to do this, or are there obviously better ways to achieve my goal?
, . dataContext, , Linq to SQL; TOP N .
dataContext
TOP N
(, "", .)
, , - ToList(). , IEnumerable<T>, , .
ToList()
IEnumerable<T>
: , , ToList. ToList, , . , . , , , .
ToList
5 , , ; 5000 . ToList(), , . .
, , .
, . 1 :
var recentItems = (from item in dataContext.Items where item.Date<=date orderby item.Date descending select item).Take(5).ToList();
.
Source: https://habr.com/ru/post/1744858/More articles:Regular expression - add target = "blank" to all links of the tag in my content - c #могу ли я получить доступ к структуре внутри структуры без использования оператора точки? - c++Using ed to manage files matching search - bashOutdated pear installation errors - phpHow to count newlines in Internet Explorer? - javascriptLine output Labview - stringРазрешение двусмысленного указателя на С++ - c++PHP does not obey my specific ETags - phpOne to two relationships in the Doctrine with YAML - mysqlGiven an array of arrays, how can I cut the substring "GB" from each value? - substringAll Articles