如何使用 LINQ 和导航属性连接表
How to join tables using LINQ and navigation properties
考虑我之前的问题(我给它加了 link)我需要得到一些不同的信息。
这里是一个DB结构我只加了导航属性
public virtual ICollection<Accident> Accidents { get; set; }
去交通class
public class Person
{
[Key]
public int PersonID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Transport
{
[Key]
public int TransportID { get; set; }
public string Model { get; set; }
public string Brand { get; set; }
public virtual ICollection<Accident> Accidents { get; set; }
}
public class Accident
{
[Key]
public int AccsidentID { get; set; }
public DateTime AccidentDate { get; set; }
public int TransportID { get; set; }
[ForeignKey("TransportID")]
public virtual Transport Transport { get; set; }
public int PersonID { get; set; }
[ForeignKey("PersonID")]
public virtual Person Person { get; set; }
}
public class AccsidentObject
{
[Key]
public int AccidentID { get; set; }
public DateTime AccidentDate { get; set; }
public int TransportID { get; set; }
public string Model { get; set; }
public string Brand { get; set; }
public int PersonID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
如果我想得到我使用的所有事故
var accidents = DBContext.Accidents.Select( a => new AccidentObject
{
AccidentID = a.AccidentId,
AccidentDate
TransportID
Model
Brand = a.Transport.Brand,
PersonID = a.Person.PersonID,
FirstName
LastName
});
如果我想 select 添加特定事故数据的 TransportObject,代码是什么
public class TransportObject
{
[Key]
public int TransportID { get; set; }
public string Model { get; set; }
public string Brand { get; set; }
public int AccidentID { get; set; }
public DateTime AccidentDate { get; set; }
}
使用此代码
var transports = DBContext.Transports
.SelectMany(
x => x.Accidents,
(t, a) => new TransportObject
{
TransportID = t.TransportID,
Model = t.Model,
Brand = t.Brand,
AccidentID = a.AccidentId,
AccidentDate = a.AccidentDate
}
);
select 更多关于 MSDN。
考虑我之前的问题(我给它加了 link)我需要得到一些不同的信息。
这里是一个DB结构我只加了导航属性
public virtual ICollection<Accident> Accidents { get; set; }
去交通class
public class Person
{
[Key]
public int PersonID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Transport
{
[Key]
public int TransportID { get; set; }
public string Model { get; set; }
public string Brand { get; set; }
public virtual ICollection<Accident> Accidents { get; set; }
}
public class Accident
{
[Key]
public int AccsidentID { get; set; }
public DateTime AccidentDate { get; set; }
public int TransportID { get; set; }
[ForeignKey("TransportID")]
public virtual Transport Transport { get; set; }
public int PersonID { get; set; }
[ForeignKey("PersonID")]
public virtual Person Person { get; set; }
}
public class AccsidentObject
{
[Key]
public int AccidentID { get; set; }
public DateTime AccidentDate { get; set; }
public int TransportID { get; set; }
public string Model { get; set; }
public string Brand { get; set; }
public int PersonID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
如果我想得到我使用的所有事故
var accidents = DBContext.Accidents.Select( a => new AccidentObject
{
AccidentID = a.AccidentId,
AccidentDate
TransportID
Model
Brand = a.Transport.Brand,
PersonID = a.Person.PersonID,
FirstName
LastName
});
如果我想 select 添加特定事故数据的 TransportObject,代码是什么
public class TransportObject
{
[Key]
public int TransportID { get; set; }
public string Model { get; set; }
public string Brand { get; set; }
public int AccidentID { get; set; }
public DateTime AccidentDate { get; set; }
}
使用此代码
var transports = DBContext.Transports
.SelectMany(
x => x.Accidents,
(t, a) => new TransportObject
{
TransportID = t.TransportID,
Model = t.Model,
Brand = t.Brand,
AccidentID = a.AccidentId,
AccidentDate = a.AccidentDate
}
);
select 更多关于 MSDN。