具有多对多的复杂 LINQ 多个表
本文关键字:LINQ 复杂 | 更新日期: 2023-09-27 18:32:44
我还是LINQ的新手。这种情况可以用 linq 做吗?
我有4张桌子:
1. Garage (ID, Name)
2. Car (ID, GarageID, Name) -- a garage has multiple cars.
3. Dealer (ID, Name)
4. Car_Dealership (ID, CarID, DealerID) -- many to many table between car and dealer
我想收集所有车库以及每个车库处理的每个经销商。
如何使用 LINQ 执行此操作?
提前感谢!
你应该能够使用这样的东西:
var map =
from garage in Garages
join car in Cars on garage.ID equals car.GarageID
join car_dealership in Car_Dealerships on car.ID equals car_dealership.CarID
join dealer in Dealers on car_dealership.DealerID equals dealer.ID
group dealer by garage;
foreach (var garage in map)
{
Console.WriteLine("Garage: " + garage.Key.Name);
foreach (var dealer in garage)
Console.WriteLine(" Dealer: " + dealer.Name);
}
将来,请发布您尝试过的内容,以便我们可以帮助您解决您遇到的特定问题。