// 針對 DbContxt 去撰寫 partial class publicpartialclassNorthwindEntities { ///<summary> /// Functions the total price. ///</summary> ///<param name="orderId">The order identifier.</param> ///<param name="productId">The product identifier.</param> ///<returns>System.Nullable<System.Decimal>.</returns> [DbFunction("NorthwindModel.Store", "fn_TotalPrice")] publicdecimal? Fn_TotalPrice(int orderId, int productId) { var parameters = new List<ObjectParameter>(2) { new ObjectParameter("OrderID", orderId), new ObjectParameter("ProductID", productId) };
var lObjectContext = ((IObjectContextAdapter)this).ObjectContext; var output = lObjectContext. CreateQuery<decimal?>("NorthwindModel.Store.fn_TotalPrice(@OrderID, @ProductID)", parameters.ToArray()) .Execute(MergeOption.NoTracking) .FirstOrDefault(); return output; }
///<summary> /// Functions the total product quan. ///</summary> ///<param name="orderId">The order identifier.</param> ///<returns>System.Nullable<System.Int32>.</returns> [DbFunction("NorthwindModel.Store", "fn_TotalProductQuan")] publicint? Fn_TotalProductQuan(int orderId) { var paramter = new ObjectParameter("OrderID", orderId); var lObjectContext = ((IObjectContextAdapter)this).ObjectContext; var output = lObjectContext. CreateQuery<int?>("NorthwindModel.Store.fn_TotalProductQuan(@OrderID)", paramter) .Execute(MergeOption.NoTracking) .FirstOrDefault(); return output; } }
使用 Order_DetailsController 來呈現
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
private NorthwindEntities db = new NorthwindEntities(); public ActionResult Index() { var order_Details = db.Order_Details.Select(o => new OrderDetailViewModel() { OrderID = o.OrderID, ProductID = o.ProductID, UnitPrice = o.UnitPrice, Quantity = o.Quantity, Discount = o.Discount,
///<summary> /// Statics the function total price. ///</summary> ///<param name="db">The database.</param> ///<param name="orderId">The order identifier.</param> ///<param name="productId">The product identifier.</param> ///<returns>System.Nullable<System.Decimal>.</returns> [DbFunction("NorthwindModel.Store", "fn_TotalPrice")] publicstaticdecimal? StaticFn_TotalPrice(this NorthwindEntities db,int orderId, int productId) { var parameters = new List<ObjectParameter>(2) { new ObjectParameter("OrderID", orderId), new ObjectParameter("ProductID", productId) };
var lObjectContext = ((IObjectContextAdapter)db).ObjectContext; var output = lObjectContext. CreateQuery<decimal?>("NorthwindModel.Store.fn_TotalPrice(@OrderID, @ProductID)", parameters.ToArray()) .Execute(MergeOption.NoTracking) .FirstOrDefault(); return output; }
///<summary> /// Statics the function total product quan. ///</summary> ///<param name="db">The database.</param> ///<param name="orderId">The order identifier.</param> ///<returns>System.Nullable<System.Int32>.</returns> [DbFunction("NorthwindModel.Store", "fn_TotalProductQuan")] publicstaticint? StaticFn_TotalProductQuan(this NorthwindEntities db, int orderId) { var paramter = new ObjectParameter("OrderID", orderId); var lObjectContext = ((IObjectContextAdapter)db).ObjectContext; var output = lObjectContext. CreateQuery<int?>("NorthwindModel.Store.fn_TotalProductQuan(@OrderID)", paramter) .Execute(MergeOption.NoTracking) .FirstOrDefault(); return output; }
一樣使用 Order_DetailsController 來呈現
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
private NorthwindEntities db = new NorthwindEntities(); public ActionResult Index() { var order_Details = db.Order_Details.ToList().Select(o => new OrderDetailViewModel() { OrderID = o.OrderID, ProductID = o.ProductID, UnitPrice = o.UnitPrice, Quantity = o.Quantity, Discount = o.Discount, // 靜態擴充方法 TotalPrice = db.StaticFn_TotalPrice(o.OrderID, o.ProductID).Value }); return View(order_Details); }