foreach referencing different rows
-
This is not necessarily a LINQ question, but maybe the better solution is LINQ specific. theWork is the result of a LINQ query. One of the fields is Date. I need the datedifference between each row. This is the way I've done it:
int theOffset = 0;
foreach (var work in theWork)
{
if (theOffset > 0)
{
theGap = work.Date - theWork.ElementAt(theOffset-1).Date;
}
theOffset++;
}Is there a better way of doing it?
-
This is not necessarily a LINQ question, but maybe the better solution is LINQ specific. theWork is the result of a LINQ query. One of the fields is Date. I need the datedifference between each row. This is the way I've done it:
int theOffset = 0;
foreach (var work in theWork)
{
if (theOffset > 0)
{
theGap = work.Date - theWork.ElementAt(theOffset-1).Date;
}
theOffset++;
}Is there a better way of doing it?
I don't know about a LINQ syntax, but I would normally store the last item in a variable instead of using ElementAt. Something like:
var lastWork;
bool notFirst;
foreach (var work in theWork)
{
if (notFirst)
{
theGap = work.Date - lastWork.Date;
}
else
{
notFirst = true;
}
lastWork = work;
} -
I don't know about a LINQ syntax, but I would normally store the last item in a variable instead of using ElementAt. Something like:
var lastWork;
bool notFirst;
foreach (var work in theWork)
{
if (notFirst)
{
theGap = work.Date - lastWork.Date;
}
else
{
notFirst = true;
}
lastWork = work;
}Looks much better.
-
This is not necessarily a LINQ question, but maybe the better solution is LINQ specific. theWork is the result of a LINQ query. One of the fields is Date. I need the datedifference between each row. This is the way I've done it:
int theOffset = 0;
foreach (var work in theWork)
{
if (theOffset > 0)
{
theGap = work.Date - theWork.ElementAt(theOffset-1).Date;
}
theOffset++;
}Is there a better way of doing it?
The LINQ way for calculating the differences would be:
DateTime[] input = {
new DateTime(2010, 1, 1),
new DateTime(2010, 2, 1),
new DateTime(2010, 3, 1)
};
var gaps = input.Skip(1).Zip(input, (a,b)=>(a-b));
foreach (TimeSpan ts in gaps)
Console.WriteLine(ts.TotalDays); // Output is: 31, 28.Zip
is new in .NET 4, but you can easily define it yourself in .NET 3.5:public static IEnumerable<TResult> Zip<T1, T2, TResult>(this IEnumerable<T1> input1, IEnumerable<T2> input2, Func<T1, T2, TResult> selector)
{
using (IEnumerator<T1> e1 = input1.GetEnumerator())
using (IEnumerator<T2> e2 = input2.GetEnumerator()) {
while (e1.MoveNext() && e2.MoveNext())
yield return selector(e1.Current, e2.Current);
}
} -
The LINQ way for calculating the differences would be:
DateTime[] input = {
new DateTime(2010, 1, 1),
new DateTime(2010, 2, 1),
new DateTime(2010, 3, 1)
};
var gaps = input.Skip(1).Zip(input, (a,b)=>(a-b));
foreach (TimeSpan ts in gaps)
Console.WriteLine(ts.TotalDays); // Output is: 31, 28.Zip
is new in .NET 4, but you can easily define it yourself in .NET 3.5:public static IEnumerable<TResult> Zip<T1, T2, TResult>(this IEnumerable<T1> input1, IEnumerable<T2> input2, Func<T1, T2, TResult> selector)
{
using (IEnumerator<T1> e1 = input1.GetEnumerator())
using (IEnumerator<T2> e2 = input2.GetEnumerator()) {
while (e1.MoveNext() && e2.MoveNext())
yield return selector(e1.Current, e2.Current);
}
}And Zip would work for any supplied formula. Very useful addition.