using System.Collections.Generic;
using Newtonsoft.Json.Linq;
public static void Main()
""book_name"": ""The Dragon"",
""book_type"": ""stories""
""book_name"": ""Mirage"",
""book_type"": ""stories""
""book_name"": ""The burning forest"",
""book_type"": ""stories""
""book_name"": ""Traditions of Latin America"",
""book_type"": ""culture""
""book_name"": ""Addis Ababa"",
""book_type"": ""culture""
DataTable dts = JsonConvert.DeserializeObject<DataTable>(json);
var groupedByUserName = (from DataRow dr in dts.Rows
user_name = dr["user_name"].ToString(),
book_name = dr["book_name"].ToString(),
book_type = dr["book_type"].ToString()
.GroupBy(x => new { x.user_name, x.book_type })
user_name = x.Key.user_name,
book_type = x.Key.book_type,
.GroupBy(x => x.user_name)
JObject jObj = new JObject();
jObj.Add("user_name", x.Key);
jObj.Merge(JObject.FromObject(x.ToDictionary(y => y.book_type, y => y.count)));
return jObj.ToObject<object>();
var apiResult = new ApiResult
Data = groupedByUserName,
Count = groupedByUserName.Count,
Console.WriteLine(JsonConvert.SerializeObject(apiResult, Formatting.Indented));
public List<dynamic> Data { get; set; }
public int Count { get; set; }
public bool Success { get; set; }