Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. foreach referencing different rows

foreach referencing different rows

Scheduled Pinned Locked Moved LINQ
questioncsharpdatabaselinq
5 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nigel Mackay
    wrote on last edited by
    #1

    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?

    G D 2 Replies Last reply
    0
    • N Nigel Mackay

      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?

      G Offline
      G Offline
      Gideon Engelberth
      wrote on last edited by
      #2

      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;
      }

      N 1 Reply Last reply
      0
      • G Gideon Engelberth

        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;
        }

        N Offline
        N Offline
        Nigel Mackay
        wrote on last edited by
        #3

        Looks much better.

        1 Reply Last reply
        0
        • N Nigel Mackay

          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?

          D Offline
          D Offline
          Daniel Grunwald
          wrote on last edited by
          #4

          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);
          }
          }

          N 1 Reply Last reply
          0
          • D Daniel Grunwald

            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);
            }
            }

            N Offline
            N Offline
            Nigel Mackay
            wrote on last edited by
            #5

            And Zip would work for any supplied formula. Very useful addition.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups