Skip to content

Instantly share code, notes, and snippets.

@vector623
Created March 5, 2018 21:41
Show Gist options
  • Save vector623/327f84ec410a6b937fbbf23c08a9292c to your computer and use it in GitHub Desktop.
Save vector623/327f84ec410a6b937fbbf23c08a9292c to your computer and use it in GitHub Desktop.
use RouteValueDictionary to de-anonymize dynamic types
using (var reportResponse = reportRequest.GetResponse())
using (var reportReader = new StreamReader(reportResponse.Stream))
using (var csv = new CsvReader(reportReader))
{
var records = csv
.GetRecords<dynamic>()
.Select(record =>
{
var recordDictionary = new RouteValueDictionary(record);
var newKeywordStat = new KeywordPerformanceStat() {
device = recordDictionary["Device"].ToString(),
date = DateTime.Parse(recordDictionary["Day"].ToString()),
campaignId = Convert.ToInt64(recordDictionary["Campaign ID"].ToString()),
adGroupId = Convert.ToInt64(recordDictionary["Ad group ID"].ToString()),
keywordId = Convert.ToInt64(recordDictionary["Keyword ID"].ToString()),
cpcBid = recordDictionary["Max. CPC"].ToString().Equals(" --") ?
(long?) null :
Convert.ToInt64(recordDictionary["Max. CPC"].ToString()),
cost = Convert.ToInt64(recordDictionary["Cost"].ToString()),
clicks = Convert.ToInt64(recordDictionary["Clicks"].ToString()),
impressions = Convert.ToInt64(recordDictionary["Impressions"].ToString()),
searchImpressionShare = recordDictionary["Search Impr. Share"].ToString().Equals(" --") ?
(decimal?) null :
Convert.ToDecimal(
recordDictionary["Search Impr. Share"]
.ToString()
.Replace("%","")
.Replace("< ","")),
allConversions = Convert.ToDecimal(recordDictionary["All conv."]),
allConversionsValue = Convert.ToDecimal(recordDictionary["All conv. value"]),
};
return newKeywordStat;
})
.ToList();
return records;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment