Skip to content

Instantly share code, notes, and snippets.

@JakeKalkman
Created January 15, 2020 19:00
Show Gist options
  • Save JakeKalkman/670d61253d99921836848bbf3198c5c1 to your computer and use it in GitHub Desktop.
Save JakeKalkman/670d61253d99921836848bbf3198c5c1 to your computer and use it in GitHub Desktop.
public void CombineResults(string groupCol, string targetCol, string differingCol)
{
var groupedResults = new Dictionary<string, IList<Dictionary<string, string>>>();
foreach(var dbRow in QueryResults)
{
if (!groupedResults.ContainsKey(dbRow[groupCol]))
{
groupedResults.Add(dbRow[groupCol], new List<Dictionary<string, string>>());
}
groupedResults[dbRow[groupCol]].Add(dbRow);
}
var combinedResults = new List<Dictionary<string, string>>();
foreach(var resultKey in groupedResults.Keys)
{
var results = groupedResults[resultKey];
var combinedResult = new Dictionary<string, string>();
foreach(var dbResult in results)
{
foreach(var key in dbResult.Keys)
{
if (!combinedResult.ContainsKey(key))
{
combinedResult.Add(key, dbResult[key]);
}
if(key == differingCol)
{
combinedResult.Add(dbResult[differingCol], dbResult[targetCol]);
}
}
}
combinedResults.Add(combinedResult);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment