How to Display Rows in Ascending Order Using SelectMany Method

Ali basha Syed 20 Reputation points
2023-08-21T11:29:17.44+00:00

Hi,

I have one method, which is returning few records using SelectMany method, but it is not returning in Ascending order.

Can someone help me in this regard, I have explained complete scenario in pdf file, code of the method is pasted below. Many Thanks in advance.

 private IEnumerable<string> FetchValues(string perforationType, bool? bdhFastInUse, Func<ValveLiftDto, string> selectParameter) =>         valveLiftsLegLengths             .Where(v => v.PerforationType == perforationType                         && (v.PerforationType != PerforationType.BDH.ToString() || v.BdhFastInUse == bdhFastInUse))             .Select(selectParameter)             .Where(value => !string.IsNullOrEmpty(value))             .SelectMany(value => value.GetCommaSeparatedList())             .Distinct();


If I select up to "Where" while debugging, I can see the rows in Ascending order, in horizontal manner.

 valveLiftsLegLengths
            .Where(v => v.PerforationType == perforationType
                        && (v.PerforationType != PerforationType.BDH.ToString() || v.BdhFastInUse == bdhFastInUse))
            .Select(selectParameter)
            .Where(value => !string.IsNullOrEmpty(value))

But if I select up to "SelectMany", I am getting rows not as Ascending, but in some different order in vertical like..

11.1 mm

12.7 mm

7.9 mm

9.5 mm

 valveLiftsLegLengths
            .Where(v => v.PerforationType == perforationType
                        && (v.PerforationType != PerforationType.BDH.ToString() || v.BdhFastInUse == bdhFastInUse))
            .Select(selectParameter)
            .Where(value => !string.IsNullOrEmpty(value))
            .SelectMany(value => value.GetCommaSeparatedList())
Developer technologies .NET Entity Framework Core
Developer technologies ASP.NET ASP.NET Core
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-08-21T15:49:03.9533333+00:00

    The result order should be each comma separated list, appended in the original order.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Ali basha Syed 20 Reputation points
    2023-08-22T04:35:34.55+00:00

    I have just removed .Order in below method and it worked.

    public static IEnumerable<string> GetCommaSeparatedList(this string values)
            => values
                .Split(",")
                .Select(value => value.Trim())
                .DistinctValues()
                .Order();
    
    0 comments No comments

  2. Ali basha Syed 20 Reputation points
    2023-08-22T04:35:44.2166667+00:00

    I have just removed .Order in below method and it worked.

    public static IEnumerable<string> GetCommaSeparatedList(this string values)
            => values
                .Split(",")
                .Select(value => value.Trim())
                .DistinctValues()
                .Order();
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.