LINQ with subselect and groupby to get only the latest version of each item in a list?

How about something like private void button1_Click(object sender, EventArgs e) { List list = GetAnswers(); var dict = (from a in list group a by a. QuestionID into grp from g in grp where g. Version == grp.

Max(m => m. Version) select new { id = g. QuestionID, q = g }).

ToDictionary( o => o. Id, o => o. Q); StringBuilder sb = new StringBuilder(); foreach (var elem in dict) { sb.

AppendLine(elem.Key.ToString() + "-" + elem.Value.Version.ToString()); } MessageBox. Show(sb.ToString()); } private List GetAnswers() { List result = new List(); result. Add(new Answer() { ID = 1, QuestionID = 1, Version = 1 }); result.

Add(new Answer() { ID = 2, QuestionID = 1, Version = 2 }); result. Add(new Answer() { ID = 3, QuestionID = 1, Version = 3 }); result. Add(new Answer() { ID = 4, QuestionID = 2, Version = 1 }); result.

Add(new Answer() { ID = 5, QuestionID = 2, Version = 2 }); result. Add(new Answer() { ID = 6, QuestionID = 2, Version = 3 }); result. Add(new Answer() { ID = 7, QuestionID = 3, Version = 1 }); result.

Add(new Answer() { ID = 8, QuestionID = 3, Version = 2 }); result. Add(new Answer() { ID = 9, QuestionID = 3, Version = 3 }); result. Add(new Answer() { ID = 10, QuestionID = 3, Version = 4 }); return result; }.

How about something like.... private void button1_Click(object sender, EventArgs e) { List list = GetAnswers(); var dict = (from a in list group a by a. QuestionID into grp from g in grp where g. Version == grp.

Max(m => m. Version) select new { id = g. QuestionID, q = g }).

ToDictionary( o => o. Id, o => o. Q); StringBuilder sb = new StringBuilder(); foreach (var elem in dict) { sb.

AppendLine(elem.Key.ToString() + "-" + elem.Value.Version.ToString()); } MessageBox. Show(sb.ToString()); } private List GetAnswers() { List result = new List(); result. Add(new Answer() { ID = 1, QuestionID = 1, Version = 1 }); result.

Add(new Answer() { ID = 2, QuestionID = 1, Version = 2 }); result. Add(new Answer() { ID = 3, QuestionID = 1, Version = 3 }); result. Add(new Answer() { ID = 4, QuestionID = 2, Version = 1 }); result.

Add(new Answer() { ID = 5, QuestionID = 2, Version = 2 }); result. Add(new Answer() { ID = 6, QuestionID = 2, Version = 3 }); result. Add(new Answer() { ID = 7, QuestionID = 3, Version = 1 }); result.

Add(new Answer() { ID = 8, QuestionID = 3, Version = 2 }); result. Add(new Answer() { ID = 9, QuestionID = 3, Version = 3 }); result. Add(new Answer() { ID = 10, QuestionID = 3, Version = 4 }); return result; }.

Exactly what I was looking for. So that's how you do it! Thanks for the prompt answer.

– Ted Jan 13 '09 at 4:29.

LatestVersionAnswers = answers . GroupBy(e => e.Question. ID) .

Select(g => g. OrderByDescending(e => e. Version).First()) .

ToDictionary(e => e.Question. ID); Or, if you prefer the selecting overload of ToDictionary: latestVersionAnswers = answers . GroupBy(e => e.Question.ID) .

ToDictionary( g => g. Key, g => g. OrderByDescending(e => e.

Version).First() ).

I would just rewrite the data model to include the current answer version ID.

Or, if you prefer the selecting overload of ToDictionary.

If I only needed the version (and not the ID, Value, etc.) I wouldn't need the OrderBy as I could just go g. Select(e => e. Version).Max() (or I've now seen the post at http://stackoverflow.com/questions/363655/c-list-groupby-2-values, but this again would only return the key/s and one property: Version).

Ultimately, in this particularly situation I would much prefer to just "filter" the original list and return the original answer items instead of involving the AnswerDTO.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions