Skip to content

Instantly share code, notes, and snippets.

@ayende
Created July 23, 2020 12:06
Show Gist options
  • Save ayende/4db8a26020a58f921c244db38d4a29d1 to your computer and use it in GitHub Desktop.
Save ayende/4db8a26020a58f921c244db38d4a29d1 to your computer and use it in GitHub Desktop.
// map
from o in docs.Orders
from l in o.Lines
select new {
l.Product,
Related = o.Lines.Select(x=>x.Product)
.Where(x=>x!=l.Product)
.Select(x=>new { Count = 1, Product = x}),
Count = 1
}
// reduce
from result in results
group result by result.Product into g
select new {
Product = g.Key,
Count = g.Sum(x=>x.Count),
Related = g.SelectMany(x=>x.Related)
.GroupBy(x=>x.Product)
.Select(x=>new {
Count = x.Sum(y=>y.Count),
Product = x.Key
})
.OrderByDescending(x=>x.Count)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment