The following LINQ query reads a delimited file and returns the most recent record for each record ID. The problem is, the most recent record does not always return what I'm doing wrong? What do I need to change to ensure that the most recent date always comes back? Is there a better way to use Mac ()?
I have also added some sample data so that you can see this issue. When viewing the sample data, I want to return the rows marked with the asterisk (*) (most recent date). Rows marked with an x get wrongly, in my opinion, back in
In cases where the same record appears many times (# 162337, for example) and there are many dates, I want a record with the most recent date.
var recipient = file. Redlines (path) Select (record => record.plip ('|')). Select (token = & gt; new {FirstName = token [2], lastname = token [4], record id = convert toInt32 (token [13]), date = convert.Todetime (token [17])}). GroupBy (m => m.recordId). OrderbendingSending (M => M.Max (x = & gt; x.date)). Select (M => M.First ()). OrderBe (M => M. Records ID). Dump (); First Name Last Name Record Date Date fname lname 137308 2/15/1991 0:00 fname lname 138011 6/16/1983 0:00 * fname lname 138011 11/9/1981 0:00 x fname lname 158680 9/4/1986 0:00 Fname lname 161775 4/23/1991 0:00 fname lname 162337 12/1/1998 0:00 * fname lname 162337 12/1/1998 0:00 * fname lname 162337 9/1/1994 0:00 x Fname lname 162337 9/1/1994 0:00 x fname lname 163254 2/12/1969 0:00 fname lname 173816 9/26/1997 0:00 fname lname 178063 1/16/1980 0:00 * fname lname 178063 3 / 3/1976 0:00 x FNL LNG 180725 7/1/2007 0:00 * FNL LNG 180725 1/14/1992 0:00 x FNN LNA 181153 5/1/2001 0:00 < / Pre>
You get the entire sequence of the group from the maximum date within each group Are Esh. What you need to do within each individual group so that only the items with the maximum date can be selected. var recipient = file. Read selected (path = & gt; record.Split ('|')) Select (token = & gt; new {first name = token [2], last name = token [4], record id = convert ToInt32 (token [13]), date = convert.Toodtime (token [17])}) .GroupBy (m = & gt; m.recordId, (k, g) = & gt; g. Orderbeadsending (m => m.date). First ()). Orderbie (m => mrcord id); If performance is important and each group can potentially contain many items, then you can probably see little improvement if you set < Maximum records in the group instead of code> composite OrderByDescending / first combo: // ... Groupbie (M => M.recordId, (k, g) = & gt; g.Agregate ((a, m) => (m.date> a.date)? M: a)) // ...
Comments
Post a Comment