多表连接查询

  • 为了查询完整的订单明细,我们需要创建“订单明细-订单”视图:
[Entity("OrderDetail", EJoinMethodBase.LeftJoin, "[Order] ON [OrderDetail].[OrderID]=[Order].[OrderID]")]
public class OrderDetailView
{
    public int? OrderDetailID { get; set; }
    public int? OrderID { get; set; }
    [EntityField("Order", "OrderNo")]
    public string OrderNo { get; set; }
    public string ItemCode { get; set; }
    public decimal? Price { get; set; }
}
  • 现在,查询N1203060001的订单明细,并显示订单编号:
var result = SimpleMapper.Inject<OrderDetailView>().Select(new OrderDetailView
{
    OrderNo = "N1203060001"
});
//输出数据:
//OrderDetailID OrderID OrderNo 	ItemCode    Price
//1             2       N1203060001	iPhone4s    5999.0000 
//2             2       N1203060001	Kindle3     1299.0000
  • 让我们看看KylinORM生成的Sql语句:
SELECT [OrderDetail].[OrderDetailID],[OrderDetail].[OrderID],[Order].[OrderNo],[OrderDetail].[ItemCode],[OrderDetail].[Price]
FROM [OrderDetail] LEFT JOIN [Order] ON [OrderDetail].[OrderID]=[Order].[OrderID] 
WHERE [Order].[OrderNo]=@OrderNo

下一页:自定义查询模板

Last edited Oct 20, 2012 at 3:34 PM by gameboyLV, version 4

Comments

No comments yet.