There is such a structure: город -> List<дом> -> List<квартира> . It is necessary to choose all cities and all houses, but to choose only those apartments that belong to the requested person.

So far, this is the case, but he chooses only where he is, which is not surprising for Any:

 cities.Where(x => x.Houses.Any(y => y.Apartments.Any(z => z.MemberId == "4"))).ToList(); 

Code for mocap, console application:

 using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp { class Program { static void Main(string[] args) { var cities = new List<City> { new City { Id = 1, Houses = new List<House> { new House { Id = 1, CityId = 1, Apartments = new List<Apartment> { new Apartment {Id = 1, MemberId = "1", HouseId = 1}, new Apartment {Id = 2, MemberId = "1", HouseId = 1}, new Apartment {Id = 3, MemberId = "2", HouseId = 1}, new Apartment {Id = 4, MemberId = "3", HouseId = 1} } }, new House { Id = 2, CityId = 1, Apartments = new List<Apartment> { new Apartment {Id = 5, MemberId = "3", HouseId = 2}, new Apartment {Id = 6, MemberId = "4", HouseId = 2} } } } }, new City { Id = 2, Houses = new List<House> { new House { Id = 3, CityId = 2, Apartments = new List<Apartment> { new Apartment {Id = 1, MemberId = "5", HouseId = 3}, new Apartment {Id = 4, MemberId = "6", HouseId = 3} } }, new House { Id = 4, CityId = 2, Apartments = new List<Apartment> { new Apartment {Id = 5, MemberId = "6", HouseId = 4} } } } } }; var a = cities.Where(x => x.Houses.Any(y => y.Apartments.Any(z => z.MemberId == "4"))).ToList(); Console.ReadKey(); } } public class City { public long Id { get; set; } public List<House> Houses { get; set; } } public class House { public long Id { get; set; } public List<Apartment> Apartments { get; set; } public long? CityId { get; set; } public City City { get; set; } } public class Apartment { public long Id { get; set; } public string MemberId { get; set; } public long HouseId { get; set; } public House House { get; set; } } } 

    1 answer 1

    If you do not touch the original collection, then

     var b = cities .Select(city => new City { Id = c.Id, Houses = city.Houses.Select(h => new House { Id = h.Id, Apartments = h.Apartments.Where(apt => apt.MemberId == "4").ToList() }).ToList() }) .Where(city=>city.Houses.Any(h=>h.Apartments.Any())).ToList() 

    If you touch, then

     cities.ForEach(city=>city.Houses .ForEach(h=>h.Apartments = h.Apartments.Where(apt=>apt.MemberId == "4").ToList())) var c = cities.Where(city=>city.Houses.Any(h=>h.Apartments.Any())).ToList(); 
    • In the first variant, it is necessary to replace Id = c.Id with Id = city.Id But both requests choose only where there are necessary apartments, but all cities and all houses should be, but apartments can be null if they do not fit the condition. As an option, you can simply delete unnecessary apartments through foreach, but I would like to use linq - Mar
    • @Jagailo well, remove the final filteration .Where(city=>city.Houses.Any(h=>h.Apartments.Any())).ToList() from requests, get houses without apartments - tym32167
    • Yes, it really works, thanks for the help. - Jagailo