score:1
The easiest way is probably to "flatten" the problem and then do the group by:
var query = officers.SelectMany(
x => new[] {
new { x.Name, RO = x.ReportingOfficer1 },
new { x.Name, RO = x.ReportingOfficer2 },
new { x.Name, RO = x.ReportingOfficer3 }
}
);
var grouped = query.GroupBy(y => y.RO);
foreach (var group in grouped) {
foreach (var item in group) {
Console.WriteLine(String.Format("{0}: {1}", item.RO, item.Name));
}
}
Here, I am assuming officers
is a IEnumerable<Officer>
where
class Officer {
public string Name { get; set; }
public string ReportingOfficer1 { get; set; }
public string ReportingOfficer2 { get; set; }
public string ReportingOfficer3 { get; set; }
}
With your sample data this is my output:
John: A
John: B
Jack: A
Rob: A
Rob: C
Earl: B
Carl: B
Carl: D
Chris: C
Kenny: C
Rodney: D
Jacob: D
Source: stackoverflow.com
Related Articles
- Group By either column using C# LINQ
- How to calculate count on of table column using group by clause in linq
- How to group an SQL date column stored as int as Year Month using LINQ
- Group by column and get first record of group using Linq
- Group DataSet Column Values into Comma Separated String using LINQ
- Group by one column and Distinct by another column using Linq
- Getting all elements while using group by on string column using LINQ
- select more column not in group by using linq in c#
- Possible to add two columns together and group by that column using linq on IQueryable?
- Get the specific count of each column using GROUP BY LINQ
- Group rows with identical value into column using LINQ
- GROUP BY in datatable on two columns with sum of third column using LINQ
- Using LinQ with group by more than one column
- Using Linq First Group particular Column value and then Make Avg of values from second column
- datatable sum column and concatenate rows using LINQ and group by on multiple columns
- Calculate sum of column using group by in Linq
- Select column from group using linq
- Convert string[] to int[] in one line of code using LINQ
- Using Linq to group a list of objects into a new grouped list of list of objects
- How to get first record in each group using Linq
- LINQ to SQL using GROUP BY and COUNT(DISTINCT)
- Select All distinct values in a column using LINQ
- Counting Using Group By Linq
- Using LINQ to group a list of objects
- Using LINQ to group by multiple properties and sum
- how to group by multiple columns using linq
- Group by variable integer range using Linq
- Exclude a column from a select using LINQ
- Linq to SQL using group By, and order by count
- Group alternate pairs using LINQ
- Is a Collection of Expression<Func<T, TProp>> possible?
- How does OfType<T>() Work?
- too many function calls
- Linq query to get data from array of objects c#
- Not able to access fields with group by and multiple joins with linq
- Linq - Query a query result agains a list of strings
- Linq Join Multiple Criteria (Number not known beforehand)
- Join two collections, taking the value from second collection (left outer join)
- Query a Many to Many to show certain Events
- OrderByDescending before AddRange Linq Query
- Question of LINQ optimisation
- How can I add Dictionary Object dynamically in LINQ?
- EntityFramework Core select M:N related data
- 2 databases with the same name exist in web.config
- LINQ string contain any element of a list of string (ASP net core MVC)
- Using Union and Contains in LINQ
- Are subqueries in LinqToSql guaranteed to be in the same order as their parent?
- How to update LINQ class before insert
- Find a random content in the database - Linq
- Entity Framework/ Linq - groupby and having clause