C
C#2y ago
SWEETPONY

❔ How to join 3 collections?

I have following logic:
var territoriesIDs = _parameters.TerritoriesIDs;

var accessGroups = await _accessGroupClient.GetByTerritoriesIds( territoriesIDs );
var accessGroupsIds = accessGroups.Select( accessGroup => accessGroup.Id );

var departments = await _departmentClient.GetByAccessGroupsIds( accessGroupsIds );
var departmentsIds = departments.Select( department => department.Id );

var subjects = await _subjectClient.GetByDepartmentsIds( departmentsIds );
var territoriesIDs = _parameters.TerritoriesIDs;

var accessGroups = await _accessGroupClient.GetByTerritoriesIds( territoriesIDs );
var accessGroupsIds = accessGroups.Select( accessGroup => accessGroup.Id );

var departments = await _departmentClient.GetByAccessGroupsIds( accessGroupsIds );
var departmentsIds = departments.Select( department => department.Id );

var subjects = await _subjectClient.GetByDepartmentsIds( departmentsIds );
I'd like to join accessGroups, departments, subjects, and get some properties from that collections How can I do it?
6 Replies
SWEETPONY
SWEETPONYOP2y ago
Also I need to group by territory
Denis
Denis2y ago
If you wish to perform an SQL like join, try this:
Enumerable.Join(listA, listB, a=>a.id, b=>b.id, (a,b) =>(a,b)).ToList()
Enumerable.Join(listA, listB, a=>a.id, b=>b.id, (a,b) =>(a,b)).ToList()
source: https://www.reddit.com/r/csharp/comments/mxvsoj/comment/gvrpzvl/?utm_source=share&utm_medium=web2x&context=3
reddit
r/csharp - Comment by u/ropewalker on ”Linq pairing two Lists of ...
16 votes and 10 comments so far on Reddit
Denis
Denis2y ago
You can use the .Join extension method to combine two lists into a new list of a different type, e.g., a value tuple. After performing your joins you can select the properties that you need, group, whatever
SWEETPONY
SWEETPONYOP2y ago
but I have 3 lists
Denis
Denis2y ago
so first join a and b then join the result with c
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?