How to find the first occurrence with LINQ

Using .NET LINQ, I would like to find records (Name) that start with a PID equal to 0 or 1. But if the name has both, I only need 0. In the following:

PID Name 0 P1 1 P1 1 P3 0 P4 0 P5 1 P5 

I will return the lines:

  0 P1 1 P3 0 P4 0 P5 

PID values ​​can increase to 10. Any suggestions on how this can be done?

+7
source share
2 answers

You can use:

 var results = collection .Where(item => item.PID == 0 || item.PID == 1) .GroupBy(item => item.Name) .Select(g => g.OrderBy(item => item.PID).First()); 
+11
source

At the end of the statement, add ".FirstOrDefault ()"

0
source

All Articles