I have a structure that roughly looks like this:
List<ProductLine> -> ID
Name
...
List<LineOfBusiness> -> ID
Name
...
List<Watchlist> -> ID
Name
ReportCount
A Watchlist can exist under multiple LoBs but the ReportCount will only be for the count of reports that exist under that LoB for that WatchList. I need them in the structure this way because the count of how many reports exist within a LoB for a given Watchlist is important elsewhere.
What I need to do is get a list of the distinct WatchLists (grouped based on ID) and have the ReportCount be the SUM of that watchlist's ReportCount across all LoBs. I can't quite get the nested select logic to work right.
The technique to flatten a hierarchical structure is to use the
SelectMany
method. You need something like this:The first
SelectMany
call will transform the original list to sequence of allLineOfBusiness
objects in all items. The secondSelectMany
call will transform a sequence ofLineOfBusiness
objects to a sequence containing allWatchlist
objects it them. Then you group theseWatchlist
s by theyID
and perform the actual query on them.