.net - linq query with aggregate in where clause -
I have a list of objects that have a start / end date if the current record is present, or if it fails, the most A recently concluded
- current has been defined which is in the past and in the future there is a null end date (open ended) or expiry date.
- There is no current time period with the maximum expiration date. In theory, time bounds marked by each object should never overlap, so I hope the maximum end date will be sufficient for the most recently ended period.
I was trying to follow the same question, but I can not find the correct entry of that entry, when there will be other entries in that list which will be dated in future (Ie start / end date where both are in the future).
My attempt is as follows, where is the start date, end date is B, C is an indicator for me, while debugging, and D is used to define the list entry type . I am interested in only 2 types.
[TestMethod] Public Zero TestRetrievalOfListElements () {var baseDate = DateTime.Now; Var list = new [] {new {a = new dateTime? (BaseDate.AddDays (-90)), b = new date time? (BaseDate.AddDays (-60)), c = "LongExpired", d = 1} new {a = new date time (base date adides (-190)), b = new date time? (BASDATE. ADDISES (-160)), C = "Longest Required", D = 1}, New {a = New Date Time? (BaseDate.AddDays (-59)), b = new date time? (Bisadet. Adidas (+20)), C = "on", d = 1}, new {a = new date time? (Basedate.adwords (-159)), b = new datetime (baseadded adiege (-91)), c = "longxide", d = 1}, new {one = new date time? (Base date add-on (-90)), b = new date time (BaseDate.AddDays (-60)), c = "LongExpired", d = 2}, new {a = new date time? (BaseDate.AddDays (-190)), b = new date time? (BaseDate.AddDays (- 160)), C = "long time exquired", d = 2}, new {a = new date time (basead.adends (-59)), b = new date time? (Bisadet. Adides (+20)), C = "present", d = 2}, new {a = new date time? (Bassadet. Additions (-159)), B = New Date Time? (Bisadet. Adygez (-91)), C = "long-expected", d = 2}, new {a = new date time? (Bisadet. Adyades (+21)), B = New DateTime? (Bassadet. Adige (+60)), C = "Future", D = 2},} .Oolist (); // The following is not really true, because it does not take account // Whether D is type 1 or 2 is not sure how to combine the set and // other conditions such as MIN and type 2 var old Entry = (Select xa == list.Min (d = & gt; da) & amp; amp; amp; and; xd == 2 in the list in x) .FirstOrDefault (); Confirm. Truth (oldest entry.a == basedate.adwords (-190); "The oldest date will be 190 days before today"); Var latest EntryThatIsntInTheFuture = (In the list y from the list with x where (z = & gt; zd == 2 & amp; amp; za & lt; = baseDate) where xa == list.Max (d => ) & Amp; amp;; Xd == 2 Select x) .FirstOrDefault (); Confirm. ISRR (Latest EntretyTISNT Influenza.A == Baseted Aid Day (-59), "The expected date to date is not 59 days earlier than today"); }
This is what you want:
// Find current which is defined as a non-empty start date which has a null end date (open end) and and expiry date in the previous // and in the future. Var now = datetime.Now; Var current = list. Where (x = & gt; x.a.hasValue & x.a.Value & lt; now & (! X.b.HasValue || x.b.Value & gt; now)) .FirstOrDefault (); If (current == zero) {// then the most recently ended current = search the list. Where (x => x.b.HasValue). Order by dyeing (x = & gt; x. B). FirstOver Default (); }
So you do not need a total of the query in my opinion.
Comments
Post a Comment