Home > Back-end >  Is the syntax for LINQ correct to return the duration
Is the syntax for LINQ correct to return the duration

Time:08-24

I am working on LINQ where I have a compile-time error and I am unable to pinpoint my mistake.

 return string.IsNullOrEmpty(allSongsDuration) ?
        new TimeSpan() :
        TimeSpan.FromSeconds(
            allSongsDuration.Split(",")
            .Select(songDurationAsString => TimeSpan.ParseExact(
                songDurationAsString, @"m\:ss", null))
                .Sum(timespan => timespan.TotalSeconds));

Above is my code which returns the error: "can not convert from 'string' to 'char'" at the line allSongsDuration.Split(",").

I am using VS 2017. Is there a version-related issue with this syntax? I am trying to split the allSongsDuration as it is separated by commas.

CodePudding user response:

You are using double quotes around the character you are passing to string.Split which makes it a string, but the single parameter overload requires a char instead which means you need to use single quotes:

allSongsDuration.Split(',')

Alternatively, you can still pass a string (with double quotes) by specifying the StringSpltOptions parameter as well:

allSongsDuration.Split(",", StringSplitOptions.None)

  • Related