Skip to content

Instantly share code, notes, and snippets.

@KennethanCeyer
Created December 13, 2016 07:45
Show Gist options
  • Save KennethanCeyer/5872d02cad683cee74e7b17a741ef7bd to your computer and use it in GitHub Desktop.
Save KennethanCeyer/5872d02cad683cee74e7b17a741ef7bd to your computer and use it in GitHub Desktop.
This is a guideline to how make to complex linq for applied the group clause in the models.
class TestModel
{
public int columnA;
public int columnB;
public int columnC;
public string columnD;
}
[HttpPost]
public JsonResult ModelLoad()
{
List<TestModel> testModelList = new List<TestModel>();
Dictionary<Tuple<int, int>, string> resultModel = new Dictionary<Tuple<int, int>, string>();
for (int i=0; i<50; i++)
{
testModelList.Add(new TestModel()
{
columnA = i % 3,
columnB = i,
columnC = i % 4,
columnD = string.Format("string {0}", i.ToString())
});
}
testModelList.GroupBy(x => new
{
x.columnA,
x.columnC
})
.ToList()
.ForEach(g =>
{
var stringList = new List<string>();
g
.ToList()
.ForEach(z =>
{
stringList.Add(z.columnB.ToString());
});
resultModel[Tuple.Create<int, int>(g.Key.columnA, g.Key.columnC)] = string.Join(", ", stringList);
});
return APPActor.GetJson(resultModel);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment