c# - Select single item from each group in multiple groups -


I have a list of specific class items (especially IEnumerable):

  Internal class MyItem {Public MyItem (Date Time Timestamp, String Code) {Timestamp = Timestamp; Code = code; } Public DateTime Timestamp {get; Private set; } Public string code {get; Private set; }}  

Within this list, there will be many items with the same code. Each will have a timestamp, which can be unique or not.

I am trying to retrieve a dictionary of mitimum ( dictionary & lt; string, MyItem & gt; ), where the key is the code related to the item.

  public dictionary & lt; String, Myitime & gt;  

Looking at this signature, how do I get MyItem with a timestamp of most time, but after that

> Latest AlgaeamTimstamp for each code ?

For example, the following is given for input:

  IEnumerable  

If the latest acceptmimestamp is 1/3/2014, the result will only include the following items:

  Timestamp | Code ---------------- 1/3/2014 | 1 1/2/2014 | 2  

I only list < Code> Latest AlpTimeTemst before filtering to those timestamps I can manage it, but I do not know that Linq is enough to take a lot lately, put each code and put it in a dictionary.

  var output = items.Where (t = & Gt; (T. teststamp & lt; = the latest Alltemestamp)). GroupB (T => T. Code);  

At this point, I ended up with two groups But do not know how to select one item in each group.

This is your share Land that you said LB should post your question (right)

  var list = new List & lt; MyItem & gt; () {New mitim () {code = "1", timestamp = new date time (2014,1,1)}, new MyItem () {code = "2", timestamp = new date time (2014,1, 2) }, New mitim () {code = "1", timestamp = new date time (2014,1,3)}, new mitim () {code = "1", timestamp = new date time (2014,1,4)}, New mitim () {code = "2", timestamp = new date time (2014,1,4)}}; Date time LatestRequest PermalinkStamp = New DateTime (2014, 1, 3);  

This is my answer

  var results = list.GroupBy (x => x.code). Select (x = & gt; x.OrderByDescending (Y = & gt; y.timestamp) .FirstOrDefault (z = & gt; z.timestamp & lt; = Latest Unlaww Timestamp)). Ollist ();  

Comments

Popular posts from this blog

HTML/CSS - Automatically set height width from background image? -

php - Mysql Show Process - Sleep Commands and what to do -

c - What is the address of buf (the local variable in the main function)? -