我想加入3个模型,但其中一个有ICollection,我不知道如何管理这个。我写了简单的SQL,它的工作,但我不知道如何执行它在代码C#使用linq。我的模型。
public class Car
{
public int IDCar { get; set; }
public virtual CarProducent Producent { get; set; }
public string Model { get; set; }
public double Engine { get; set; }
public int HorsePower { get; set; }
public DateTime ProductionDate { get; set; }
}
public class CarProducent
{
public int IDCarProducent { get; set; }
public string producent { get; set; }
}
public class Klient
{
public int IDKlient { get; set; }
public string name{ get; set; }
public string surname { get; set; }
public string phone { get; set; }
public string address { get; set; }
public virtual ICollection<Car> Cars{ get; set; }
}
我想把这3个模型连接起来,以获得它的全部细节。我写的那种查询
var car = ( from cars in dataContext.Cars
join prod in dataContext.CarProducent on cars.Producent.IDCarProducent equals prod.IDCarProducent
join client in dataContext.Clients on cars.IDCar equals client.Cars. /*Here I don't know how to join */
where cars.IDCar == id
select new
{
cars.IDCar,
cars.Model,
cars.Engine,
cars.HorsePower,
cars.ProductionDate,
prod.producent,
client.name,
client.surname,
client.address,
client.phone
}).FirstOrDefault();
我只是在学习,所以任何建议都会有帮助。
这是简单的
from client in dataContext.Klient
from car in client.Cars
where cars.IDCar == id
select new
{
car.IDCar,
car.Model,
car.Engine,
car.HorsePower,
car.ProductionDate,
car.Producent.producent,
client.name,
client.surname,
client.address,
client.phone
}
如果您添加了另一个导航属性,您也可以写上
from car in dataContext.Cars
where cars.IDCar == id
select new
{
car.IDCar,
car.Model,
car.Engine,
car.HorsePower,
car.ProductionDate,
car.Producent.producent,
car.Client.name,
car.Client.surname,
car.Client.address,
car.Client.phone
}