Answered by:
How to get distinct month and year from the data table using LINQ query.

Question
-
User1610382325 posted
Dim dt as DataTable = obj.getdates()
DATE 03/10/2015 11/11/2015 12/12/2015 08/09/2015 04/04/2015 02/07/2015 08/07/2015 i want to get a result like this.
Apr-15 Jul-15 Sep-15 Oct-15 Nov-15 Dec-15 Any help.
Thanks,
PonrajWednesday, December 23, 2015 12:02 PM
Answers
-
User1577371250 posted
Hi,
what is the error?
here is the VB.NET code
Dim dt As New DataTable() dt.Columns.Add("JoinedDate", GetType(DateTime)) dt.Rows.Add(Convert.ToDateTime("03/10/2015")) dt.Rows.Add(Convert.ToDateTime("11/11/2015")) dt.Rows.Add(Convert.ToDateTime("12/12/2015")) dt.Rows.Add(Convert.ToDateTime("08/09/2015")) dt.Rows.Add(Convert.ToDateTime("04/04/2015")) dt.Rows.Add(Convert.ToDateTime("02/07/2015")) dt.Rows.Add(Convert.ToDateTime("08/07/2015")) Dim monthList = (From r In From p In dt.AsEnumerable()Let month = p.Field(Of DateTime)("JoinedDate").MonthGroup p By monthOrder
By r.Key AscendingNew With { _ Key .items = r.[Select](Function(c) c.Field(Of DateTime)("JoinedDate").ToString("MMM yyyy")).FirstOrDefault() _ }).ToList()- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, December 28, 2015 5:52 AM
All replies
-
User1577371250 posted
Hi,
Check this query
DataTable dt = new DataTable(); dt.Columns.Add("JoinedDate", typeof(DateTime)); dt.Rows.Add(Convert.ToDateTime("03/10/2015")); dt.Rows.Add(Convert.ToDateTime("11/11/2015")); dt.Rows.Add(Convert.ToDateTime("12/12/2015")); dt.Rows.Add(Convert.ToDateTime("08/09/2015")); dt.Rows.Add(Convert.ToDateTime("04/04/2015")); dt.Rows.Add(Convert.ToDateTime("02/07/2015")); dt.Rows.Add(Convert.ToDateTime("08/07/2015")); var monthList = (from p in dt.AsEnumerable() let month = p.Field<DateTime>("JoinedDate").Month group p by month into r orderby r.Key ascending select new
{
items = r.Select(c=> c.Field<DateTime>("JoinedDate").ToString("MMM yyyy")).FirstOrDefault()
}).ToList();Wednesday, December 23, 2015 2:11 PM -
User1610382325 posted
I converted to VB.NET and tried code giving error.
Friday, December 25, 2015 8:28 AM -
User1577371250 posted
Hi,
what is the error?
here is the VB.NET code
Dim dt As New DataTable() dt.Columns.Add("JoinedDate", GetType(DateTime)) dt.Rows.Add(Convert.ToDateTime("03/10/2015")) dt.Rows.Add(Convert.ToDateTime("11/11/2015")) dt.Rows.Add(Convert.ToDateTime("12/12/2015")) dt.Rows.Add(Convert.ToDateTime("08/09/2015")) dt.Rows.Add(Convert.ToDateTime("04/04/2015")) dt.Rows.Add(Convert.ToDateTime("02/07/2015")) dt.Rows.Add(Convert.ToDateTime("08/07/2015")) Dim monthList = (From r In From p In dt.AsEnumerable()Let month = p.Field(Of DateTime)("JoinedDate").MonthGroup p By monthOrder
By r.Key AscendingNew With { _ Key .items = r.[Select](Function(c) c.Field(Of DateTime)("JoinedDate").ToString("MMM yyyy")).FirstOrDefault() _ }).ToList()- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, December 28, 2015 5:52 AM