Home > Back-end >  Does my LINQ query provide distinct SampleNumbers based off the code and LINQPad output provided?
Does my LINQ query provide distinct SampleNumbers based off the code and LINQPad output provided?

Time:01-19

I'm trying to use LINQPad to see outputs of my queries I'm writing for a C# WinForms application. I'm more just confused on how to interpret the outcome on a Dump() method and what is happening behind the scenes.My intended outcome is to have one SampleNumber per group of SampleData. SampleData is coming out of the database as a string split by commas. Also, please spare me; I know how I name stuff is horrible and I'm planning to adjust once I get the data coming out correctly!

Here is my initial query:

var thinthroughoutQuery = (from a in ThicknessBlobs
                                       join b in SummaryDataItems on a.SampleNumber equals b.SampleNumber
                                       where (a.Source == "Thickness[0,0]")
                                       && b.TopBottomStatusFK == 3
                                       orderby a.PKId descending
                                       select a).Take(1000).ToList();

Here is where I attempt to take the string of SampleData, cut it at the delimiter, group by SampleNumber, and parse it as an integer:

var intThinThroughoutQuery = thinthroughoutQuery.Select(row => new { SampleNumber = row.SampleNumber, Value = row.SampleData.Split(",").Select(int.Parse) })
                .SelectMany(group => group.Value.Select(value => new { group.SampleNumber, SampleData = value })).ToArray();

Here is the output from using Dump() in LINQPad:

Dump output from intThinThroughoutQuery

To me, this appears like there are not distinct SampleNumbers and that each piece of SampleData separately maps to a repeating SampleNumber.

For further clarification, I want to be able to access the data and have it be like this:

Intended output

Rather than:

enter image description here

CodePudding user response:

You are missing GroupBy/ToLookup after flattening with SelectMany:

var intThinThroughoutQuery = thinthroughoutQuery.Select(row => new { SampleNumber = row.SampleNumber, Value = row.SampleData.Split(",").Select(int.Parse) })
    .SelectMany(group => group.Value.Select(value => new { group.SampleNumber, SampleData = value }))
    .ToLookup(row => row.SampleNumber, row => row.SampleData);
  • Related