Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Split time List into time ranges

Tags:

c#

.net

list

linq

I am looking to extract ranges from an list of integers using linq:

for example I am looking to split the following list:

List<int> numberList = new List<int>() { 30, 60, 90, 120, 150, 180, 270, 300, 330 };  

into a list of integer ranges that will look like:

{ 30, 180 }
{ 270, 330 }

ie: where the next seq is greater than 30

another example :

List<int> numberList = new List<int>() { 30, 60, 120, 150, 270, 300, 330 };  

into a list of integer ranges that will look like:

{ 30, 60 }
{ 120, 150 }
{ 270, 330 }

I have tried with for loops to find the best way possible however I don't know where to start trying to use a linq query to do this.

like image 432
Wattcey Avatar asked Jun 04 '13 22:06

Wattcey


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

You could write a method to handle the split:

IEnumerable<IList<int>> SplitValues(IList<int> input, int difference = 30)
{
    List<int> results = new List<int>();
    int last = input.First();
    foreach(var value in input)
    {
        if (value - last > difference)
        {
            yield return new[] {results.First(), results.Last()};
            results = new List<int>();
        }

        results.Add(value);
        last = value;
    }

    yield return new[] {results.First(), results.Last()};
}

This matches your specifications as described, returning:

{ 30, 60 }
{ 120, 150 }
{ 270, 330 }

Note that a single value within the collection without a range will be duplicated. For example, { 30, 120, 150 } will return:

{ 30, 30 }
{ 120, 150 }
like image 163
Reed Copsey Avatar answered Sep 30 '22 00:09

Reed Copsey


You can do this in one linq statement:

var numberList = new List<int>() { 30, 60, 120, 150, 270, 300, 330 };
var section = 0;
var result = numberList
            .Select( (x, i) => new {value = x, section = (i == 0 ? 0 : ((x - numberList[i - 1]) > 30 ? ++section : section))})
            .GroupBy(x => x.section)
            .Select(x => x.Select(v => v.value).ToList()).ToList();
like image 43
rivarolle Avatar answered Sep 30 '22 00:09

rivarolle