复杂的LINQ多表有多对多

时间:2013-09-24 21:20:42

标签: c# linq

我还是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执行此操作?

提前致谢!

1 个答案:

答案 0 :(得分:0)

你应该能够使用这样的东西:

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);
}

将来,请发布您尝试过的内容,以便我们为您遇到的具体问题提供帮助。