My data starts in a JSON object:
JSON
{"page":"1",
"per_page":10,
"total":100,
"total_pages":10,
"data":[{"id":1,
"userId":1,
"userName":"Jim Silver",
"timestamp":16425382171,
"txnType":"debit",
"amount":"$1,000.07",
"location":{"id":2,
"address":"654, Somewhere, Some Street",
"city":"Some City",
"zipCode":12345},
"ip":"202.210.105.105"},
{"id":2," ...}
{"id":3," ...}
...
{"id":n," ...}]
I need to get the user ID and amount into a List<list<int>> which I've done here:
var model = JsonConvert.DeserializeObject<TxnResponse>(rawJSON);
var elements = model.data;
List<List<int>> userTxns = new List<List<int>>(elements.Count);
List<int> record = null;
int userID = 0;
int userAmount = 0;
for (int row = 0; row < elements.Count; row++)
{
userID = elements[row].userId;
userAmount = Convert.ToInt32(Math.Floor(decimal.Parse(model.data[row].amount, System.Globalization.NumberStyles.Currency)));
record = new List<int>();
record.Add(userID);
record.Add(userAmount);
userTxns.Add(record);
}
return userTxns.GroupBy();
The result should look like this when printed to screen (Console.WriteLine(String.Join("\n", list.Select(x => String.Join(" ", x))));):
1 1000
2 2000
3 3000
4 4000
Without the Group By, if the JSON object has 10 data items, I have 10 (userId, amount) records.
Model:
public class TxnResponse
{
public string page { get; set; }
public int per_page { get; set; }
public int total { get; set; }
public int total_pages { get; set; }
public List<data> data { get; set; }
}
public class data
{
public int id { get; set; }
public int userId { get; set; }
public string userName { get; set; }
public object timestamp { get; set; }
public string txnType { get; set; }
public string amount { get; set; }
public Location location { get; set; }
public string ip { get; set; }
}
public class Location
{
public int LocationID { get; set; }
public string Address { get; set; }
public string City { get; set; }
public int ZipCode { get; set; }
}
How do I do GroupBy on user ID? Thank you.
What I have tried:
return userTxns.GroupBy(userID);
return userTxns.GroupBy(u => userID);
but get "the type arguments for method cannot be inferred from the usage..." or "Cannot implicitly convert type...".