0

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!

mharre
  • 233
  • 3
  • 17
  • Are you saying that `driveFileList` is a `List` and `MiscFile.Permissions` is type `List` and `MiscObject2.Domain` is type `string`? What exactly are you trying to get back? A list of `MiscFile` objects or a list of `MisObject2` objects? Are you trying to get all the `MiscObject2` objects that have the specified `Domain` value, regardless of which `MiscFile` they belong to? – John Aug 10 '22 at 02:31
  • I think you need .SelectMany instead of .Select, give it a try. If that's what you need I suggest check this post for a detailed explanation https://stackoverflow.com/questions/958949/difference-between-select-and-selectmany – 4D1C70 Aug 10 '22 at 02:49
  • driveFileList is being returned from another method. What it is, is a List. Object1 contains a string fileName, string fileId and List. Object2 is a list of permissions. I want to add Object1 to an entirely new list but based on if Object2.Permissions.Domain == "insertdomainhere.com" – mharre Aug 10 '22 at 03:53

1 Answers1

2

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 difference, 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.

John
  • 3,057
  • 1
  • 4
  • 10
  • Hi John sorry for the very confusing explanation, I am not the best at explaining. Essentially what I want to do is filter based on the permissions. If they match a certain domain then I want to add them to an entirely new list of objects. With SelectMany I get a type conversion exception but this is mainly because of how I need to write tests. So we have driveFileList that I want to map to a permissionList. But from driveFileList I want to extract what I mentioned above. I think my main issue is the type conversion but thank you this gets me closer – mharre Aug 10 '22 at 03:04