Can you just write:
table.Select(z => new DateTime(z.LastYear, z.LastMonth, z.LastDay));
Ok, I tested this on Linqpad, and it seems that Linq to SQL decides to create some kind of weird character conversion query. I'm not quite sure if this is the source of performance, but you can force such a projection:
var dates =
(from z in table
select new { Year = z.LastYear, Month = z.LastMonth, Day = z.LastDay })
.AsEnumerable()
.Select(d => new
{
Date = new DateTime(d.Year, d.Month, d.Day),
NextDate = new DateTime(d.Year, d.Month, 1).AddMonths(2).AddDays(-1)
});
It will also give you the last day of the next month.
If you really see such a huge performance, but, of course, I will risk assuming that you are looking in the wrong place, and that there is something else. 90% of all database performance problems are related to bad or non-existent indexes or intermittent queries.
source
share