0
        List<Object> lstsData = new List<object>();
        try
        {
            var thisWeekStart = Date.AddDays(-(int)Date.DayOfWeek);
            var thisWeekEnd = thisWeekStart.AddDays(7).AddSeconds(-1);
            var q = from c in db.Tasks select c.TaskName;
            var Taskss = db.Tasks.Where(x => x.IsActive == true && x.TaskName != null).Select(x => x.TaskName);
            var HourSum = db.TimesheetData.Where(x => x.Date >= thisWeekStart && x.Date <= thisWeekEnd).GroupBy(x => x.TaskID).
            Select(grp => new { /*TaskID = grp.Key,*/ HourSum = grp.Sum(x => (double?)x.Hours ?? 0) });
            lstsData.Add(new { Hourss = HourSum, Taskname = q });
            return Json(lstsData);
        }

My issue is when i am trying return json then my data placed inside array. This is my console data

     {Hourss: Array(3), Taskname: Array(3)}
     length
     :
     1
   __proto__
    :
    Array(0)

My issue is when i am trying return json then my data placed inside array.

Y.verma
  • 11
  • 5

2 Answers2

0
lstsData.Add(new { Hourss = HourSum, Taskname = q });

HourSum and q are still IQueryable form. You should execute them to retrieve data from database.

Just try like this;

lstsData.Add(new { Hourss = HourSum.ToList(), Taskname = q.ToList() });
lucky
  • 12,734
  • 4
  • 24
  • 46
  • not working same value come that i given into my question. – Y.verma Dec 22 '17 at 11:14
  • What would you expect ? Of course the returned Hourss and Taskname data stored in an array. For example you could navidate it like response.Hourss[0] or iterate it like response.Hourss – lucky Dec 22 '17 at 11:20
  • He just ask a bad question he didn't know how to do a join for the two arrays. – Filip Cordas Dec 22 '17 at 11:22
0

I am almost certain you what is this query.

  var resultData = from task in db.Tasks
                                 where task.IsActive == true && task.TaskName != null
                                 join timesheet in db.TimesheetData on task.Id equals timesheet.TaskId
                                 where timesheet.Date >= thisWeekStart && timesheet.Date <= thisWeekEnd
                                 group new { task, timesheet } by new { task.Id, task.TaskName } into taskTime
                                 select new { TaskName = taskTime.Key.TaskName, Hours = taskTime.Sum(tt => tt.timesheet.Hours) };

return Json(resultData.ToArray());

What you want is to join the two tables based on the Id. The JSON serialization turns all ienumerable/iqueryable into JSON arrays. You just didn't write a proper query to return the data.

Filip Cordas
  • 2,531
  • 1
  • 12
  • 23
  • @Flip Cordas Thanks a lot my issue is solved you write a correct query. – Y.verma Dec 22 '17 at 11:22
  • @Y.verma You should try to rewrite the question because next time people might not be able to figure out what problem are you having and asking a good question is important if you want to use SO. – Filip Cordas Dec 22 '17 at 11:24