EntityFramework를 사용하여 DB 엔터티의 중첩 목록을 채우기 위해 단일 LINQ 쿼리 만 사용하고 싶습니다.EF6 중첩 목록에 대한 단일 LINQ 쿼리
3 개의 테이블 엔티티가 있습니다. 퍼스트 클래스 Cities
은 List<Houses>
이고 Houses
은 List<Residents>
입니다.
그 클래스 :
var cities = (from city in db.Cities
select new // Creating anonymous type to fill List of Houses
{
CityId = city.CityId,
Name = city.Name,
Houses = db.Houses.Where(h=>h.CityId == city.CityId)
.Select(new // Another anonymous type, but this time this isn't working
{
HouseId = h.HouseId,
Address = h.Address,
Residents = db.Residents.Where(r=>r.HouseId == h.HouseId).ToList()
}).ToList()
.Select(h => new Houses
{
HouseId = h.HouseId,
Address = h.Address,
Residents = h.Houses
}).ToList()
})
.ToList()
.Select(c=> new Cities
{
CityId = c.CityId
Name = c.Name,
Houses = c.Houses
}).ToList()
불행하게도 나는 오류
The entity or complex type Houses cannot be constructed in a LINQ to Entities
무엇입니까 :
class Cities
{
long CityId {get;set;}
string Name {get;set;}
List<House> Houses {get;set;}
}
class Houses
{
long CityId {get;set;}
string Address {get;set;}
List<Resident> Residents {get;set;}
}
class Residents
{
long HouseId {get;set;}
string FirstName {get;set;}
string LastName {get;set;}
}
내가 달성하고자하는 것은이 같은 것입니다.
그것은 단지 Houses = db.Houses.Where(h=>h.CityId ==city.CityId).ToList()
에 대해 작동합니다. 하지만 그걸로 을 Houses
에 풀고 있습니다.
하나의 LINQ 쿼리로 할 수 있습니까?
당신이 LINQ 쿼리로 DB에 쓰기를 시도하고 있습니까? – Piou
'거주자 = h.Houses'는 오타가되어야합니다. 또한 단일 클래스 이름을 사용하는 것이 좋습니다. –