Reputation: 669
I have a Linq statement which concats three list
s together:
var FinshedResults = QualificationList.Zip(SubjectList, (x, y) => x + y).Zip(GradeList, (x, y) => x + y);
The out put this is giving me is:
A Level - Test 1 (A*) A Level - Test 2 (A) A Level - Test 3 (B) A Level - Test 4 (C) A Level - Test 5 (D) A Level - Test 6 (E) A Level - Test 6 (E) A Level - Test 7 (F)
The A Level is stored in QualificationList
I was just wondering whether we can make it so A - Level is printed out once and the rest have Test 1 etc. So the out put would more be like;
A Level - Test 1 (A) Test 2 (A) Test 3 (B) Test 4 (C) Test 5 (D) Test 6 (E) Test 6 (E) Test 7 (F)
Is it possible to make QualificationList
only print out once? Or would this have to be handled in the loop? Thanks for any advice which you can give.
Upvotes: 0
Views: 108
Reputation: 101731
Try something like this:
QualificationList
.GroupBy(x => x)
.SelectMany(g => new[] { g.Key }.Concat(Enumerable.Repeat("", g.Count() - 1))
.Zip(SubjectList, (x, y) => x + y)
.Zip(GradeList, (x, y) => x + y);
I'm assuming QualificationList
is a list of strings. This query simple groups QualificationList
and then returns a sequence of elements for each group where the first element is the Key
(for example A Level) the rest is an empty string. Then just uses Zip
like you did, the only difference is rest of the elements are concatenated with empty string so the A Level
won't appear more than once.
Upvotes: 1