We have a heavy traffic data in DB, we don't want to dump huge data on to the client side and do grouping and aggregating.
So I started doing it on server side pagination, grouping and stuck with one column grouping. Need help with support multiple column grouping and then also able to aggregate data on server side.
Here is my grid Read Method
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Read_Traffic([DataSourceRequest] DataSourceRequest request) { var results = _auditService.All(OperatingUser.CompanyId, request, out int totalRecords); if (request.Groups != null && request.Groups.Any()) { //HaBo: Currently supporting only single grouping. //need to work on to support multiple groupings. return Json(new DataSourceResult { Data = CustomGroupBy(results, request.Groups[0].Member), Total = totalRecords }); } //List<object> returnData = results.Cast<object>().ToList(); //foreach (var groupDescriptor in request.Groups) //{ // returnData = (List<object>) CustomGroupBy(results, groupDescriptor.Member); //} return Json(new DataSourceResult { Data = results, Total = totalRecords // Total number of records }); }
Here is my Grouping Method
private static IEnumerable<object> CustomGroupBy(List<Audit> results, string groupKey) { switch (groupKey) { case "SessionId": return results.GroupBy(x => x.SessionId).Select(g => new { Aggregates = new { }, Key = g.Key, HasSubgroups = false, Member = "SessionId", Subgroups = new object[] { }, Items = g }); case "AuditId": return results.GroupBy(x => x.AuditId).Select(g => new { Aggregates = new { }, Key = g.Key, HasSubgroups = false, Member = "AuditId", Subgroups = new object[] { }, Items = g }); case "UserName": return results.GroupBy(x => x.UserName).Select(g => new { Aggregates = new { }, Key = g.Key, HasSubgroups = false, Member = "UserName", Subgroups = new object[] { }, Items = g }); case "IpAddress": return results.GroupBy(x => x.IpAddress).Select(g => new { Aggregates = new { }, Key = g.Key, HasSubgroups = false, Member = "IpAddress", Subgroups = new object[] { }, Items = g }); case "UrlReferrer": return results.GroupBy(x => x.UrlReferrer).Select(g => new { Aggregates = new { }, Key = g.Key, HasSubgroups = false, Member = "UrlReferrer", Subgroups = new object[] { }, Items = g }); case "TimeAccessed": return results.GroupBy(x => x.TimeAccessed).Select(g => new { Aggregates = new { }, Key = g.Key, HasSubgroups = false, Member = "TimeAccessed", Subgroups = new object[] { }, Items = g }); case "CountryName": return results.GroupBy(x => x.CountryName).Select(g => new { Aggregates = new { }, Key = g.Key, HasSubgroups = false, Member = "CountryName", Subgroups = new object[] { }, Items = g }); case "CountryCode": return results.GroupBy(x => x.CountryCode).Select(g => new { Aggregates = new { }, Key = g.Key, HasSubgroups = false, Member = "CountryCode", Subgroups = new object[] { }, Items = g }); } return null; }
As you can notice currently I am only able to group with the first group member, how can I support multiple column grouping?