Reputation: 4266
I'm trying to use Linq to merge some list items.
I have a list of custom object that contains some variables : string1, string2 double1 date1.
I want to group items of the list that have string2 as common string and the double1 must be added.
Finaly, I want to save the recent date of the final item.
For the first string (string1), it doesn't matter, I can imagine that we can simply concat all the strings ?
This is an example of datas before and after the process :
"toto" "test" 0.10 Datetime(04-04-2013)
"to" "test" 0.80 Datetime(01-01-2013)
"toto" "teeeest" 0.10 Datetime(01-01-2013)
"toto" "test" 0.10 Datetime(02-01-2013)
"toto" "test" 1.00 DateTime(04-04-2013)
"to" "teeeest" 0.10 DateTime(01-01-2013)
So I can't use correctly linq but I tried to use "groupby" with a select enclosed but it doesn't run :-/
Thanks in advance to help me!
Upvotes: 0
Views: 89
Reputation: 60493
var myList = new List<MyObject>();
var result =
myList.GroupBy(x => x.string2)
.Select(g => new MyObject {
string1 = string.Join(",", g.Select(v => v.string1)),
//or g.Min(v => v.string1)
//or g.First(v => v.string1)
//or whatever you want
string2 = g.Key,
double1 = g.Sum(v => v.double1),
date1 = g.Max(v => v.date1)
});
Upvotes: 4