Add dash between more than two consecutive numbers and comma between other numbers in array I have an ordered array of integer values: 10, 11, 12, 13, 15, 16, 19
and I need to write a dash between first and last consecutive values when there are more than two consecutive values (10-13). Between other values I need to write a comma and whitespace.
Output for this input must be 10-13, 15, 16, 19.
I wrote a method that implements this:

class Program
{
    private static string AddDashesBetweenConsecutiveNumbers(int[] orderedArray)
    {
        if (orderedArray.Length == 0 || orderedArray == null)
        {
            return string.Empty;
        }
        var groupedArray = orderedArray.Select((x, i) =&gt; new { Difference = i - x, Value = x })
                .GroupBy(x =&gt; x.Difference)
                .Select(group =&gt; group.Select(a =&gt; a.Value))
                .Select(arr =&gt; arr.Count() &gt; 2
                            ? $"{arr.First()}-{arr.Last()}"
                            : string.Join(", ", arr));
        return string.Join(", ", groupedArray);
    }
    static void Main(string[] args)
    {
        int[] array = { 10, 11, 12, 13, 15, 16, 19 };

        Console.WriteLine(AddDashesBetweenConsecutiveNumbers(array)); // Output: "10-13, 15, 16, 19"
    }
}


What I wrote seems to work, but this code has some disadvantages, especially in  readability of the LINQ expression. So if you have any ideas how to improve this method or have better or alternative solution, that would be great to find out.
Sorry but no comments (yet) on the LINQ.

Your first conditional has a logic error.  You should be checking if orderedArray is null before you check if its length is 0.

The method has a leap of faith that someone is inputting an array sorted in ascending order.  There is nothing in your code to prevent otherwise.

What if someone wanted to pass in a List or IEnumerable?  Restricting input to an array is fairly limiting.  

The variable groupedArray is not an array, so the name is misleading.  A quick reworking of all of the above is:

private static string AddDashesBetweenConsecutiveNumbers(IEnumerable&lt;int&gt; values)
{
    // Avoid Count() since it requires full list enumeration.
    // FirstOrDefault() would return default(int) or 0, which is not null.
    // Any() will find the first one without a full list enumeration.
    if (values == null || !values.Any())
    {
        return string.Empty;
    }

    // If the input values is already sorted, this will be fast.
    // If the input values is not already sorted, good thing we do it here.
    var orderedValues = values.OrderBy(x =&gt; x);

    var groupedValues = orderedValues.Select((x, i) =&gt; new { Difference = i - x, Value = x })
            .GroupBy(x =&gt; x.Difference)
            .Select(group =&gt; group.Select(a =&gt; a.Value))
            .Select(arr =&gt; arr.Count() &gt; 2
                        ? $"{arr.First()}-{arr.Last()}"
                        : string.Join(", ", arr));

    return string.Join(", ", groupedValues);
}


I'm sure you are capable of writing a non-LINQ version if you desired.  I include a version below.

private static string AddDashesBetweenConsecutiveNumbers2(IEnumerable&lt;int&gt; values)
{
// Avoid Count() since it requires full list enumeration.
// FirstOrDefault() would return default(int) or 0, which is not null.
if (values == null || !values.Any())
{
    return string.Empty;
}

// If the input values is already sorted, this will be fast.
// If the input values is not already sorted, good thing we do it here.
var orderedValues = values.OrderBy(x =&gt; x);

return string.Join(", ", GetGroupedIntegers(orderedValues));
}

private static IEnumerable&lt;string&gt; GetGroupedIntegers(IOrderedEnumerable&lt;int&gt; orderedValues)
{
var sublist = new List&lt;int&gt;();

Func&lt;string&gt; getSubListOutput = () =&gt;
{
    switch (sublist.Count)
    {
        case 1:
            return sublist.First().ToString();
        case 2:
            return sublist.First().ToString() + ", " + sublist.Last().ToString();
        default:
            return sublist.First().ToString() + "-" + sublist.Last().ToString();
    }
};

foreach (var value in orderedValues)
{
    if (sublist.Count == 0 || sublist.Last() == value - 1)
    {
        sublist.Add(value);
    }
    else
    {
        yield return getSubListOutput();
        sublist = new List&lt;int&gt;() { value };
    }
}

yield return getSubListOutput();
}


Got to run to a meeting, but another flaw in both your original and my answer is that there is no check for distinct integers, but that can easily be done.

UPDATE:
Corrected code as per comments.