While this may not answer your question, it's really important to tell you that you shouldn't be doing this at all, for two reasons.
Writing readable code is very very important, and your existing code is as good as it can get. A complex one-liner will not only make it less readable but also make debugging harder. Overall maintenance is a nightmare.
You aim to bring a side effect rather than return a value after some computation. In other words your delegate returns void
. A Linq style query is not the way to go here.
And two suggestions:
I saw ArrayList
in your code. It is criminal to use it since .NET 2. See this. You will be hailed as a hero if you change it to a List<T>
. If your friend is smart he should help you with such basic things rather than being pedantic.
A name that starts with Get...
is not a good name for a method (or a delegate) that returns (gets) nothing (void
). I would call it WorkingDaysPrinter
or so.
Just for the sake of it, your answer would be something like:
WorkingDaysPrinter p = (d1, d2) => Enumerable.Range(0, d2.Subtract(d1).Days)
.Select(x => d1.AddDays(x))
.Where((x, i) => i == 0 || x.DayOfWeek != DayOfWeek.Sunday)
.ToList()
.ForEach(x =>
{
if (x.DayOfWeek == DayOfWeek.Saturday)
Console.WriteLine();
else
Console.Write(x.Day + " ");
});
It's slightly better to write the query separately and then run a separate foreach
:
WorkingDaysPrinter p = (d1, d2) =>
{
var query = Enumerable.Range(0, d2.Subtract(d1).Days)
.Select(x => d1.AddDays(x))
.Where((x, i) => i == 0 || x.DayOfWeek != DayOfWeek.Sunday);
foreach (var day in query)
{
if (x.DayOfWeek == DayOfWeek.Saturday)
Console.WriteLine();
else
Console.Write(x.Day + " ");
});
}
By the looks of it I think it would be better if your delegate returns a list of working days. Something like:
public delegate IEnumerable<DateTime> WorkingDaysGetter(DateTime x, DateTime y);
WorkingDaysGetter g = (d1, d2) => Enumerable.Range(0, d2.Subtract(d1).Days)
.Select(x => d1.AddDays(x))
.Where(x => x.DayOfWeek != DayOfWeek.Saturday && x.DayOfWeek != DayOfWeek.Sunday);
foreach (var day in g(dt1, dt2))
{
Console.Write(x.Day + " ");
}