1

I want to get students who fulfill the following criteria in a WPF application-

  1. Are "passed" the examination
  2. Except first "passed" student in each "group" (Example: Group 1 & 3)
  3. If a group contains only one "passed" student, that student should be omitted (Example: Group 2)
  4. Original grouping should be preserved

Please refer this Students collection as I don't find any option to create tables in StackOverflow.

Code I tried-

 var results = myList
                 .GroupBy(x => x.GroupID)
                 .Select(g => g.OrderBy(x => x.Status)
                 .Where(g => g.Status == "Passed")
                 .Skip(1)
                 .ToList();

The Problem: This doesn't omit the first "passed" student in each group. Instead, it omits only "Richard" (in Group 1) in the entire collection.

1
  • 1
    The real problem is that your current code can't even compile, because there's a missing ) - opened here .Select( and never closed Commented Feb 10, 2020 at 8:27

2 Answers 2

5

Well, let's try to apply conditions one after one:

var result = myList
  .Where(student => student.Status == "Passed") // passed only
  .GroupBy(student => student.GroupId)          // grouped by Group Id
  .SelectMany(group => group.Skip(1))           // Skip 1st student in each group 
  .ToList();                                    // if we want to have a List   

If

"If a group contains only one "passed" student, that student should be omitted"

means that we should skip 1st student in each group except groups with single student:

var result = myList
  .Where(student => student.Status == "Passed") 
  .GroupBy(student => student.GroupId)          
  .SelectMany(group => group.Skip(group.Count() > 1 ? 1 : 0))
  .ToList();           
Sign up to request clarification or add additional context in comments.

8 Comments

Did YoKidYo write, and Dmitry read "that student should be omitted" as "that student shouldn't be omitted"? Or is my understanding of "omitted" wrong?
No @Rafalon, he read it right. That student should be omitted.
I'm sorry, but if he should be omitted, isn't it the same as skipped? Which would mean you skip 1 regardless the Count?
Therefore, group.Count() > 1 ? 1 : 0 should be 1
@YoKidYo Well that works, but it's the same as 1 alone (just remove the whole group.Count() > 1 ? 1 : 1 and write 1 instead -> Skip(1))
|
1

Do the Skip in the select so it applies to each group, not the overall results:

 var results = myList.GroupBy(x => x.GroupID)
                    .Where(g => g.Status == "Passed")
                    .Select(g => g.OrderBy(x => x.Status).Skip(1))                 
                    .ToList();

1 Comment

.Where(g => g.Status == "Passed") doesn't work, you have a IEnumberable of items within that group so you need to filter .Where(g => g.All(y => y.Status == "Passed")) or filter it prior like Dmitry did

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.