The problem is your selection part, here:
.Select(y => new
{
startdates = y.Select(k =>
new {startdate = (k.startDate.Month.ToString() + "/" + k.startDate.Day.ToString() + "/" + k.startDate.Year.ToString()),
users = y.Select(z =>
new {userId = z.userId,
userName = z.userName})})});
You've got far too much nesting there. You're creating a startdate
part for each element within the group.
It's unclear why you're using grouping by three separate parts at all, but I suspect this will do what you want:
db.Users
.Where(x => (x.startDate >= startDate) && (x.startDate <= endDate))
.GroupBy(x => x.startDate.Date) // Or just x.startDate
.AsEnumerable() // Do the rest of the query locally
.Select(group => new
{
startdate = group.Key.ToString("MM/dd/yyyy"),
users = group.Select(z => new { z.userId, z.userName })
});
If you need to wrap that in a startdates
field, you can then use:
var result = new { startdates = query.ToArray() };