.net 当其中一个模型具有ICollection列表时,实体框架将连接3个表
我想加入3个模型,但其中一个有ICollection,我不知道如何处理。我编写了简单的SQL,它可以工作,但我不知道如何使用linq在代码c#中执行它。我的模特.net 当其中一个模型具有ICollection列表时,实体框架将连接3个表,.net,entity-framework,linq,.net,Entity Framework,Linq,我想加入3个模型,但其中一个有ICollection,我不知道如何处理。我编写了简单的SQL,它可以工作,但我不知道如何使用linq在代码c#中执行它。我的模特 public class Car { public int IDCar { get; set; } public virtual CarProducent Producent { get; set; } public string Model { get; set; } public double Eng
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; }
}
我只是在学习,所以任何建议都会有帮助。这很简单
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
}
非常感谢,我甚至没想到我能这样做,一切正常。:)
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
}