I have tables that contain a lot of rows, each record is excellent by whom and on what date was added. I want to group all records, based on AddedBy and AddedOn fields.
The problem is that the dates are in full including the miliseconds and I only want to group by day, month or year.
I hereby noted that at Compilation time I do not know which table it is and Therefore I use with System.Dynamic.LINQ library.
Below I demonstrate how did a grouping by datetime using System.Dynamic.LINQ library:
Dim groupColl= myColl.GroupBy("new(AddedName, AddedOn), it").Select("new(Key.AddedName, Key.AddedOn, Count() AS Count)")
But the problem is that I need to grouping by day, month or year.
In sql server I found how to do it, by day or by month or by year. In Lambda Expression also found the way how to do it, but only by day.
But through System.Dynamic.LINQ not find any way how to do it.
Below I demonstrate how I'm doing this in sql server and Lambda Expression:
Using SQL SERVER:
SELECT AddedBy, DATEADD(DAY, DATEDIFF(DAY, 0, AddedOn), 0) as AddedOn, count(*)
FROM myTable
GROUP BY AddedBy, DATEADD(DAY, DATEDIFF(DAY, 0, AddedOn), 0)
Using Lambda Expression vb.net code
Dim groupColl = myCollection.GroupBy(Function(x) New With {Key x.AddedBy, Key .AddedeOn_Day = DbFunctions.TruncateTime(x.AddedOn)}).Select(Function(y) New With {y.Key.AddedBy, y.Key.AddedeOn_Day, y.Count})
I would be grateful to anyone who would help me how to do it using System.Dynamic.LINQ Library
Thanks