I am trying to GROUPBY Date in a LINQ Query and display the output as shown below
startdates: [
startdate: “4/1/2014”,
users: [
{userId, …},
{userId, …}
],
startdate: “4/2/2014”, users: [
{userId, …}
],
…
]
The code is shown below
db.Users
.Where(x => (x.startDate >= startDate) && (x.startDate <= endDate))
.GroupBy(x => new { x.startDate.Day, x.startDate.Month, x.startDate.Year })
.ToList()
.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})})});
Even though the Users are Grouped by StartDate, the output contains the startDate multiple times the same number of times as the number of Users.The output is shown below. I tried putting .Distinct() but it still repeats the startdate. Can someone please help?
[{"startdates":
[{"startdate":"04/01/2014",
"users":[
{"userId":1},"userName":"John"}
{"userId":2},"userName":"Mike"}],
[{"startdate":"04/01/2014",
"users":[
{"userId":1},"userName":"John"}
{"userId":2},"userName":"Mike"}],
[{"startdate":"04/02/2014",
"users":[
{"userId":3},"userName":"AL"}
{"userId":4},"userName":"Test"}],
[{"startdate":"04/02/2014",
"users":[
{"userId":3},"userName":"AL"}
{"userId":4},"userName":"Test"}]
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() };
See more on this question at Stackoverflow