Home > Mobile >  Linq - accessing a nested list inside of an object
Linq - accessing a nested list inside of an object

Time:08-10

I hope I explained that correctly. I want to add a returned list of objects to a new list of objects based on certain restrictions. The structure for driveFileList is a list of an object which has another List inside. Essentially it is

public record MiscFile(string fileName, string fileId, List<MiscObject2>)

So far what I have tried is

permissionList.AddRange(driveFileList.Select(f => f.Permissions.Where(p => p.Domain == "domainhere.com"))); 

But I am not sure how to formulate it in a correct way.

I get a type conversion error and if I try an explicit cast it will still throw an exception. Is this possible with a linq expression or should I just change it entirely?

Any help is appreciated.

Thanks!

CodePudding user response:

I'll change this answer if my assumption turns out to be wrong but I think that you want a list of all the child objects that meet certain criteria, regardless of which parent they belong to. In that case, you can use SelectMany on the parent. That will get a list of values for each parent and concatenate them all into one list. You can then filter that flattened list:

permissionList.AddRange(driveFileList.SelectMany(f => f.Permissions)
                                     .Where(p => p.Domain == "domainhere.com"));

SelectMany will return a list of all children from all parents, then the Where filters that child list.

EDIT:

Actually, I just realised that you can have the Where outside or inside the SelectMany. I'm not sure that it would make any practical different, as you have to check every child either way, but this would work too:

permissionList.AddRange(driveFileList.SelectMany(f => f.Permissions.Where(p => p.Domain == "domainhere.com")));

That will filter each child list and then concatenate, while the previous code would concatenate the full lists and then filter. This is closer to what you had originally, but the result would be the same and I think the performance should be basically the same too, not that you could notice any different unless the lists were huge.

  • Related