[英]EF Query Help. Difficult query with interhited types and self referencing
我對EF還是陌生的,我已經找到了一些基本的查詢。 我為自己想要實現的更困難的事情之一感到困惑。
我有4個實體:用戶,租戶,建築物,公寓
租戶繼承自用戶
此時,可以將非租戶的用戶視為房東。
用戶(房東)擁有一對多建築物
建築物有一對多的公寓
公寓有一對多租戶(在任何給定時間有一個租戶)
我試圖弄清楚如何創建EF查詢,該查詢將為我提供給定用戶(房東)的租戶列表。
給我我想要的SQL是:
SELECT u2.User_ID AS TenantUser_ID, u2.UserName
FROM Users u
LEFT JOIN rt_UserBuilding ub ON u.User_ID = ub.User_ID
LEFT JOIN Buildings b ON ub.Building_ID = b.Building_ID
LEFT JOIN Apartments a ON a.Building_ID = b.Building_ID
LEFT JOIN Tenants t ON a.Apartment_ID = t.Apartment_ID
LEFT JOIN Users u2 ON t.User_ID = u2.User_ID
WHERE u.User_ID = 1 AND t.User_ID IS NOT NULL
假設您的Building
班級有財產
User Landlord
最簡單的方法是這樣做:
context.Tenants.Where(tenant => tenant.
Apartment.
Building.
Landlord.Id == yourLandlord.Id
).ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.