using System.Collections.Generic;
var searchResults = CreateSource();
from srGroup in searchResults.GroupBy(sr => sr.article)
let prices = srGroup.SelectMany(sr => sr.prices).ToDictionary(p => p.Key, p => p.Value)
let article = srGroup.First().article
select new PricedDrug { article = article, prices = prices };
foreach (var res in finalResults)
foreach (var price in res.prices)
Console.Write(price.Key + " " + price.Value + " ");
Console.WriteLine("~ " + res.article);
public static IEnumerable<PricedDrug> CreateSource()
return new List<PricedDrug> {
new PricedDrug { prices = new Dictionary<string, float> { { "site1", 100 } }, article = "1" },
new PricedDrug { prices = new Dictionary<string, float> { { "site2", 110 } }, article = "1" },
new PricedDrug { prices = new Dictionary<string, float> { { "site3", 10 } }, article = "2" },
new PricedDrug { prices = new Dictionary<string, float> { { "site2", 12 } }, article = "2" },
public class PricedDrug: IComparable<PricedDrug>
prices = new Dictionary<string, float>();
public Dictionary<string, float> prices;
public int CompareTo(PricedDrug other)
return article.CompareTo(other.article);