Question: 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.

Answer: 
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();

```
Try this:

```
private static List<int[]> GetGroups(List<int> numberList)
{
    List<List<int>> groups = new List<List<int>>();
    numberList.Zip(numberList.Skip(1), (a, b) =>
    {
        if ((b - a) == 30)
        {
            if (groups.Count == 0)
                groups.Add(new List<int>());
            groups[groups.Count - 1].Add(a);
        }
        else if (a == b)
        {
            groups[groups.Count - 1].Add(a);
        }
        else
        {
            groups[groups.Count - 1].Add(a);
            groups.Add(new List<int>());
        }
        return a;
    }).ToList();
    groups[groups.Count - 1].Add(numberList.Last());
    return groups.Select(g => new[] { g.First(), g.Last() }).ToList();
}

```

Sample usage:

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

```