1

I have a start date and an end date (in sql server datetime format). I want to split this into several ranges ie several pairs of start and end date values. NOTE - I have .NET 3.5 and Visual studio 2008.

Eg. S = 2005. E = 2010, Chunk size = 1 year. Paris generated = 2005-06, 06-07, 07-08, 08-2010

The chunks can be of any number of days/months. I put the code in an SO post, after my main method and I get some errors. Post - Split date range into date range chunks

Code -

public static IEnumerable<Tuple<DateTime, DateTime>> SplitDateRange(DateTime start, DateTime end, int dayChunkSize)
{
    DateTime chunkEnd;
    while ((chunkEnd = start.AddDays(dayChunkSize)) < end)
    {
        yield return Tuple.Create(start, chunkEnd);
        start = chunkEnd;
    }
    yield return Tuple.Create(start, end);
}

I get two errors:

The body of 'CodeHere.csproj.ScriptMain.SplitDateRange(System.DateTime, System.DateTime, int)' cannot be an iterator block because 'IEnumerable

And:

The name 'Tuple' does not exist in the current context

Community
  • 1
  • 1
Steam
  • 9,368
  • 27
  • 83
  • 122
  • 1
    Well the problem is that `Tuple` isn't found - have you imported the `System` namespace? Are you using v4.0 of .NET or later? – Jon Skeet Oct 29 '13 at 21:22
  • I used using System.Tuple and yet I get the error. Not sure which version of .NET i got. But, I am guessing its 3.XX – Steam Oct 29 '13 at 21:23
  • 1
    No, you should have `using System;` - a `using` directive names a namespace, not a type (unless you're creating an alias). But you really need to find out which version of .NET you're using. Which version of Visual Studio are you using? And have you looked in your project properties to see what your project is targeting? – Jon Skeet Oct 29 '13 at 21:24
  • @JonSkeet - I use visual studio 2008. okay, NET is vers 3.5. I already have using System; – Steam Oct 29 '13 at 21:25
  • Right. Well that's the problem. `Tuple` wasn't introduced until .NET 4. – Jon Skeet Oct 29 '13 at 21:27
  • @JonSkeet - So what do I do now ? How do I split date range ? – Steam Oct 29 '13 at 21:27
  • @blasto - See my answer for a way of doing this in .NET 3.5... :) – Faraday Oct 29 '13 at 21:44
  • Random post - in case you want to do split a range by year, then there is sql server code for this at - http://social.msdn.microsoft.com/Forums/en-US/d85290a8-8063-4268-88aa-683f8e0be83c/how-to-split-a-date-range-into-separate-years?forum=transactsql – Steam Oct 29 '13 at 21:54
  • @leppie: OP is limited to .NET 3.5 so I kept tag [[tag:.net-3.5]]. – abatishchev Dec 19 '14 at 22:32
  • @abatishchev which is pretty much the bottom end of .net on running OS's ;p The question text specification is enough IMO. Also trivial to create your own `Tuple` implementation. – leppie Dec 19 '14 at 22:36

2 Answers2

5

You're trying to use System.Tuple<T1, T2>, which was only introduced in .NET 4. You're using .NET 3.5, so that structure isn't available. I suggest you create your own DateRange type which encapsulates a start and end DateTime, then return an IEnumerable<DateRange> instead.

Either that, or upgrade to .NET 4 or 4.5...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • But Jon, the bigger problem is that I am new to C#. So, I need some more hints on how to go about it. I am not asking for full code, but some pointers or steps would be great. Can you give me some ? – Steam Oct 29 '13 at 21:31
  • @blasto: Yes: get a good introductory C# book. That's a *much* better way of learning C# as a beginner than grabbing code from the internet and then not understanding the errors you're getting. It may feel like you're making less progress at first - because you should start off with console apps, staying well away from databases etc - but you'll build up a solid foundation on which you can learn whatever you need. – Jon Skeet Oct 29 '13 at 21:35
  • I was under the impression that my strong knowledge of Java, and Java's similarity to C# would serve me well here. But, it does not seem like that exactly anymore. Indeed, I was hoping to grab code and figure it out. How similar is C# to Java ? a ballpark figure would help. Like 50%, 90% ? – Steam Oct 29 '13 at 21:39
  • 4
    @blasto: C# 1 was similar to Java 1.4. After that, they diverged significantly. But it's more than just the language - it's the idioms, the base libraries etc. There's a lot to get used to, and you really would be best off beginning at the beginning. Hopefully with knowledge of Java you can go pretty quickly through the early stuff, but it's still worth doing. – Jon Skeet Oct 29 '13 at 21:47
1

Here is a .NET 3.5 way of doing things. As others have said, Tuple didn't exist in .NET 3.5..

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var myDateTime = new DateTime(2013, 10, 29);
            var myEndDateTime = new DateTime(2014, 10, 29);
            var result = SplitDateRange(myDateTime, myEndDateTime, 10);

            var file = new System.IO.StreamWriter("c:\\dateFile.txt");
            foreach (var item in result)
            {
                file.WriteLine("StartDate: {0}, EndDate: {1}", item.StartDateTime, item.EndDateTime);
            }

            file.Close();

        }
        public static IEnumerable<SplitDateTime> SplitDateRange(DateTime start, DateTime end, int dayChunkSize)
        {
            DateTime chunkEnd;
            while ((chunkEnd = start.AddDays(dayChunkSize)) < end)
            {
                yield return new SplitDateTime(start, chunkEnd);
                start = chunkEnd;
            }
            yield return new SplitDateTime(start, end);
        }
    }
    public class SplitDateTime
    {
        public SplitDateTime(DateTime startDateTime, DateTime endDateTime)
        {
            StartDateTime = startDateTime;
            EndDateTime = endDateTime;
        }
        public DateTime StartDateTime { get; set; }
        public DateTime EndDateTime { get; set; }
    }
}
Faraday
  • 2,904
  • 3
  • 23
  • 46
  • I pressed debugging button. I see a black screen for a few seconds and then some status messages in black. The last message was - The program '[4132] SplitDateRangeCSharp.vshost.exe: Managed' has exited with code 0 (0x0). – Steam Oct 29 '13 at 21:48
  • This is just a console app to show you the functionality. You can take the SplitDateRange function and the SplitDateTime class and use it in your own solution. Console apps run to completion, as there is nothing blocking it, it exits. Put the parts I mentioned into your own app to see it working... – Faraday Oct 29 '13 at 22:19
  • Vijay - can you also show me how to print the generated ranges to a text file. That would be helpful. Thanks. – Steam Oct 29 '13 at 22:23
  • @blasto - I've changed my example to output the data to a file. – Faraday Oct 29 '13 at 23:08
  • Thanks. Your answer works. I got it. I tried this and it works. + was missing - file.WriteLine(item.StartDateTime.ToString() + ", " + item.EndDateTime.ToString()); – Steam Oct 29 '13 at 23:32
  • You would be better off with - file.WriteLine("{0}, {1)", item.StartDateTime, item.EndDateTime); – Faraday Oct 29 '13 at 23:34
  • 1
    It works almost exactly the same as Java's string format (val placeholder = "Date one %s,, date two %s" val formatted = placeholder.format("01/01/1001", "02/02/2002")) – Faraday Oct 29 '13 at 23:36