Skip to content

Instantly share code, notes, and snippets.

@sheastrickland
Created July 13, 2011 11:54
Show Gist options
  • Save sheastrickland/1080173 to your computer and use it in GitHub Desktop.
Save sheastrickland/1080173 to your computer and use it in GitHub Desktop.
Alternate GroupBy's
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Slumber.Tests
{
public class MagicData
{
public Department[] Departments { get; set; }
}
public class Department
{
public string Name { get; set; }
public string Url { get; set; }
public Section[] Sections { get; set; }
}
public class Section
{
public string Name { get; set; }
public string Url { get; set; }
public ProductGroup[] ProductGroups { get; set; }
}
public class ProductGroup
{
public string Name { get; set; }
public string Url { get; set; }
}
[TestClass]
public class Unicorns
{
public List<Tuple<string, string, string>> Setup()
{
return new List<Tuple<string, string, string>>
{
new Tuple<string, string, string>("dep1", "sec1","grp1"),
new Tuple<string, string, string>("dep1", "sec1","grp2"),
new Tuple<string, string, string>("dep1", "sec1","grp3"),
new Tuple<string, string, string>("dep1", "sec2","grp1"),
new Tuple<string, string, string>("dep2", "sec1","grp2"),
new Tuple<string, string, string>("dep2", "sec1","grp3"),
};
}
public void Assertions(MagicData magicData)
{
Assert.AreEqual(2, magicData.Departments.Length);
Assert.AreEqual("dep1", magicData.Departments[0].Name);
Assert.AreEqual(2, magicData.Departments[0].Sections.Length);
Assert.AreEqual("sec1", magicData.Departments[0].Sections[0].Name);
Assert.AreEqual(3, magicData.Departments[0].Sections[0].ProductGroups.Length);
Assert.AreEqual("grp1", magicData.Departments[0].Sections[0].ProductGroups[0].Name);
Assert.AreEqual("grp2", magicData.Departments[0].Sections[0].ProductGroups[1].Name);
Assert.AreEqual("grp3", magicData.Departments[0].Sections[0].ProductGroups[2].Name);
Assert.AreEqual("sec2", magicData.Departments[0].Sections[1].Name);
Assert.AreEqual(1, magicData.Departments[0].Sections[1].ProductGroups.Length);
Assert.AreEqual("grp1", magicData.Departments[0].Sections[1].ProductGroups[0].Name);
Assert.AreEqual("dep2", magicData.Departments[1].Name);
Assert.AreEqual(1, magicData.Departments[1].Sections.Length);
Assert.AreEqual("sec1", magicData.Departments[1].Sections[0].Name);
Assert.AreEqual(2, magicData.Departments[1].Sections[0].ProductGroups.Length);
Assert.AreEqual("grp2", magicData.Departments[1].Sections[0].ProductGroups[0].Name);
Assert.AreEqual("grp3", magicData.Departments[1].Sections[0].ProductGroups[1].Name);
}
[TestMethod]
public void Method1()
{
var allData = Setup();
var result = new MagicData
{
Departments = allData
.GroupBy(t1 => t1.Item1)
.Select(t1 => new Department
{
Name = t1.Key,
Sections = t1.GroupBy(t2 => t2.Item2)
.Select(t2 => new Section
{
Name = t2.Key,
ProductGroups = t2.GroupBy(t3 => t3.Item3)
.Select(t3 => new ProductGroup
{
Name = t3.Key,
}).ToArray()
}).ToArray()
}).ToArray()
};
Assertions(result);
}
[TestMethod]
public void Method2()
{
var allData = Setup();
var result = new MagicData
{
Departments = allData
.GroupBy(t1 => t1.Item1, (deptname, sections) => new Department
{
Name = deptname,
Sections = sections.GroupBy(t2 => t2.Item2, (sectionname, productGroups) => new Section
{
Name = sectionname,
ProductGroups = productGroups.Select(t3 => new ProductGroup {Name = t3.Item3}).Distinct().ToArray()
}).ToArray()
}).ToArray()
};
}
[TestMethod]
public void OriginalMethod()
{
var allData = Setup();
var groupedByDepartmentAndSection = allData
.GroupBy(data => new { Department = data.Item1, Section = data.Item2 })
.Select(departments => new
{
departments.Key.Department,
departments.Key.Section,
ProductGroups = departments.Select(y => y.Item3)
});
var groupedByDepartment = groupedByDepartmentAndSection.GroupBy(z => z.Department);
var result = new MagicData
{
Departments = groupedByDepartment.Select(z => new Department
{
Name = z.Key,
Sections = z.Select(y => new Section
{
Name = y.Section,
ProductGroups = y.ProductGroups.Select(x => new ProductGroup
{
Name = x
}).ToArray()
}).ToArray()
}).ToArray()
};
Assertions(result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment