The output from a groupby operation is something like a "list of groupings" and a grouping is something like a "list of the original objects plus a Key property that they all share as the same"
So as a simpler example
[
{name:John, age:23},
{name:Jim, age:23},
{name:Jack, age:24}
]
"Group By age" produces
[
{Key: 23, [{name:John, age:23},{name:Jim, age:23}]},
{Key: 24, [{name:Jack, age:24}]},
]
It's a bit hard to represent in "JSON" because the Grouping (the object with the Key property) is-a list, rather than has-a list
So, in summary "list of X in, grouped by X.Y, produces list-of-grouping and a grouping itself is a list-of-X-with-Key-of-same-X.Y"
In your code:
var noteGrouped = noteList.GroupBy(a => a.Date )
.Select(a => new
{
Date= a.Select(x=>x.Date ),
noteWithSameDateList= new {
NoteID = a.Select(x=>x.NoteID ),
Text= a.Select(x => x.Text)
}
}).ToList();
List<GroupedNoteModel> noteGroupList = noteGrouped;
I find it best to always think carefully about what to call the parameter to the lambda - you've just called yours "a" in both cases, but one is a NoteModel and one is a Grouping. Remember that your grouping are "enumerable with a Key property, which is what was grouped/what they all share ie a DateTime in your case"
For starters this means you should have "note" or "n" in your groupby (because it is operating on NoteModels) and "grp" or "g" in your first select (because is operating on Groupings, which are lists of NoteModels)
Also your Date property in your output model should definitely be the Key of the grouping
NoteWithSameDate should be a conversion of the "list of notes" (which is what a grouping is) into a "list of groupitemmodel"
var noteGrouped = noteList
.GroupBy(n => n.Date ) //n is a NoteModel
.Select(grp => new GroupedNoteModel() //you'd made an anonymous type here when you wanted to make one of these
{
Date = grp.Key,
noteWithSameDateList= grp.Select(note => //grp is a "list" of NoteModel. 'note' here is a single notemodel
new GroupItemModel() //not an anonymous type
{
NoteID = note.NoteId,
Text= note.Text
}
).ToList() //turn. grp.Select(note... into a list
}
).ToList(); //turn. .Select(grp... into a list
Final minor note, earlier in the answer I talked about the output from a groupby being a list of grouping and a grouping being a list of your original objects. I use the term list colloquially to refer to some enumerable collection; it isn't literally a List<IGrouping>
and an IGrouping isn't literally a List<T>
- it's just easier to understand the phrase "list of lists" than the crazy amount of generic types involved in a group by ("an ienumerable of ienumerable igroupings")
There are other overloads of GroupBy that are more complex; they take lambdas themselves to perform conversions of the input objects directly to output without follow up Select operations, but I recommend wrapping your head around the simple "group by date, get a list of list-of-those-with-same-date" for now..