开发者

Concat and/or merge objects in two generic lists

i need help with this problem... I have two lists of same objects "SearchResult", this object has an id attribute and a score attribu开发者_运维百科te. In both list can exist the same id, but with different score, so i need to create a new List that combines same id's but the score must be the sum of both scores and finally, the list get ordered by that final score. How can i do that? I search for a linq query, but i cant find any solution.

I hope you can help me, thanks!!!


Sounds like you might want something like:

var summed = results1.Concat(results2)
                     .GroupBy(r => r.Id)
                     .Select(g => new SearchResult { 
                                Id = g.Key, 
                                Score = g.Sum(x => x.Score)
                             });


You can use Union method from link. Example:

var mergedList = firstList.Union(secondList);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜