My LINQ query:
Dim groupedData = (From p In pData _
Group By p.TruncParam Into Group) _
.SelectMany(Function(g) g.Group) _
.Select(Function(d, idx) New With { _
.NewParameter = String.Concat(If(d.TruncParam.Length < 5, d.TruncParam, d.TruncParam.Substring(0, 5)), idx.ToString("0000")), _
.FullParameter = String.Format("{0}-{1} [{2}] <{3}>", d.LabName, d.TestName, d.Parameter, d.Unit)})
Produces these results:
ID: SOLUB0000 Name: 001-AMT SOLUBL [SOLUBLES] <%>
ID: SOLUB0001 Name: CHEM-C4:SOL [SOLUBLES] <%>
ID: SOLUB0002 Name: CHEM-EMCARB:SOL [SOLUBLES] <%>
ID: INSOL0003 Name: 001-AMT:INSOL [INSOLUBLES] <%>
ID: INSOL0004 Name: CHEM-AMT:INSOL [INSOLUBLES] <%>
ID: INSOL0005 Name: CHEM-W:INSOL [INSOLUBLES] <%>
ID: INSOL0006 Name: CHEM-W:INSOL [INSOLUBLES] <mg/l>
ID: CLRES0007 Name: 001-CL RESIDUE [CL RESIDUE] <ppm>
ID: SUMCA0008 Name: 001-ELEMENTS [SUM CA K NA SI] <%>
ID: SUMME0009 Name: 001-ELEMENTS [SUM METALS + P] <%>
When the TruncParam changes, I would like the index (idx) in the Select clause to reset to 1. So, in the list above, the Index should be SOLUB0001, SOLUB0002, INSOL0001, INSOL0002…CLRES0001, SUMCA0001, SUME0001.
How should I alter the LINQ query?
You need to move the index selection inside the
SelectManystatement (g.Group.Select(d, idx) => new {d, idx}) like this: