C# 使用Linq将动态查询结果与EntitySet连接起来?
我有以下型号:C# 使用Linq将动态查询结果与EntitySet连接起来?,c#,entity-framework,linq,C#,Entity Framework,Linq,我有以下型号: public class Order { public int Id {get; set;} public int CustomerId {get; set;} public virtual Category Category {get; set;} //Many more properties... } public class OrderLine { public int Id {get; set;} public int O
public class Order
{
public int Id {get; set;}
public int CustomerId {get; set;}
public virtual Category Category {get; set;}
//Many more properties...
}
public class OrderLine
{
public int Id {get; set;}
public int OrderId {get; set;}
public virtual Order Order {get; set;}
public virtual Product Product {get; set;}
//Other properties...
}
我需要得到某个客户的订单。为了不检索太多信息,我创建了一个类:
public class CustomerOrder
{
public int CustomerId {get; set;}
public int OrderId {get; set;}
public string ProductName {get; set;}
public virtual ICollection<OrderLine> {get; set;}
}
公共类客户订单
{
public int CustomerId{get;set;}
公共int-OrderId{get;set;}
公共字符串ProductName{get;set;}
公共虚拟ICollection{get;set;}
}
我有Order
和OrderLine
类的映射配置,但没有CustomerOrder
类的映射配置,因为我认为可以将数据投影到此类中
我可以:
CustomerOrder
类中。但是,这会强制EF检索主表和包含表的所有列吗Order
表(可能直接从视图)检索所需的详细信息。使用Linq将此结果集与OrderLine
连接,以获得完整的投影。但是,我是否需要视图的映射配置为了避免列过多并加入SQL select语句,将数据投影到
CustomerOrder
中的最佳方法是什么?您可以按如下所示进行操作。您还必须对模型进行一些更改。我已经这样做了。请参见
public class Order
{
public int Id {get; set;}
public int CustomerId {get; set;}
public virtual Category Category {get; set;}
public virtual ICollection <OrderLine> OrderLines {get; set;}
//Many more properties...
}
public class OrderLine
{
public int Id {get; set;}
public int OrderId {get; set;}
public virtual Order Order {get; set;}
public virtual Product Product {get; set;}
//Other properties...
}
public class CustomerOrder
{
public int CustomerId {get; set;}
public int OrderId {get; set;}
public string ProductName {get; set;}
public virtual ICollection<OrderLine> OrderLines {get; set;}
}
a1:否。仅从数据库中获取投影列
<强>最佳方法:总是使用自定义投影(如<代码> CustomerOrder < /代码>)。当我们考虑EF查询的性能时,这是最好的。您也可以使用它来向视图发送数据(它就像DTO(数据传输对象))。p> 您可以按如下所示进行操作。您还必须对您的模型进行一些更改。我已经完成了。请看一下
public class Order
{
public int Id {get; set;}
public int CustomerId {get; set;}
public virtual Category Category {get; set;}
public virtual ICollection <OrderLine> OrderLines {get; set;}
//Many more properties...
}
public class OrderLine
{
public int Id {get; set;}
public int OrderId {get; set;}
public virtual Order Order {get; set;}
public virtual Product Product {get; set;}
//Other properties...
}
public class CustomerOrder
{
public int CustomerId {get; set;}
public int OrderId {get; set;}
public string ProductName {get; set;}
public virtual ICollection<OrderLine> OrderLines {get; set;}
}
a1:否。仅从数据库中获取投影列
<强>最佳方法:总是使用自定义投影(如<代码> CustomerOrder < /代码>)。当我们考虑EF查询的性能时,这是最好的。您也可以使用它来向视图发送数据(它就像DTO(数据传输对象))。p> 订单和订单行之间有导航属性?@YacoubMassad Yes。如果操作正确,EF查询中的投影将避免加载所有列。请参阅:@Ivan MarkDebono,你能在问题中显示该属性吗?你在订单和订单行之间有一个导航属性?@YacoubMassad是的。如果操作正确,EF查询中的投影将避免加载所有列。看:@Ivan MarkDebono,你能展示一下问题中的财产吗?