public void Multliple_method_calls_linetotal() { using (var db = new FakeMyDbContext()) { var sods = new List <SalesOrderDetail>(); for (int i = 0; i < 10; i++) { sods.Add(new SalesOrderDetail() { SalesOrderId = 71774, ProductId = 905, OrderQty = 4, UnitPrice = 218.454m }); } db.SalesOrderDetails.AddRange(sods); using (Log.Logger.BeginTimedOperation("Calculating quick total", "Test")) { _calcLinePriceCounter.Reset(); var results = db.SalesOrderDetails .Where(sod => sod.SalesOrderId == 71774) .ToList() .Select(sod => new { sod.OrderQty, sod.UnitPrice, LineTotal = CalculateLinePrice( sod.OrderQty, sod.UnitPrice, _calcLinePriceCounter) }).ToList(); _calcLinePriceCounter.Write(); } } }
public void OneTimeSetUp() { _fakeContext = new FakeMyDbContext { Categories = { new Category { CategoryId = 123, CategoryName = "Flowers" } } }; }
public void Multliple_method_calls_margin() { using (var db = new FakeMyDbContext()) { var sods = new List <SalesOrderDetail>(); for (int i = 0; i < 10; i++) { sods.Add(new SalesOrderDetail() { SalesOrderId = 71774, ProductId = 905, OrderQty = 4, UnitPrice = 218.454m }); } db.SalesOrderDetails.AddRange(sods); var products = new Product() { ProductId = 905, StandardCost = 199.3757m }; db.Products.Add(products); using (Log.Logger.BeginTimedOperation("Calculating margin slowly", "Test")) { _calcLinePriceCounter.Reset(); _calcLineCostCounter.Reset(); _calcMarginCounter.Reset(); var results = db.SalesOrderDetails .Where(sod => sod.SalesOrderId == 71774) .Join(db.Products, sod => sod.ProductId, product => product.ProductId, (sod, product) => new { sod, product }) .ToList() .Select(li => new { li.sod.OrderQty, li.sod.UnitPrice, li.product.StandardCost, LineTotal = CalculateLinePrice( li.sod.OrderQty, li.sod.UnitPrice, _calcLinePriceCounter), LineCost = CalculateLineCost( li.sod.OrderQty, li.product.StandardCost, _calcLineCostCounter), Margin = CalculateMargin( CalculateLineCost( li.sod.OrderQty, li.product.StandardCost, _calcLineCostCounter), CalculateLinePrice( li.sod.OrderQty, li.sod.UnitPrice, _calcLinePriceCounter), _calcMarginCounter) }).ToList(); _calcLinePriceCounter.Write(); _calcLineCostCounter.Write(); _calcMarginCounter.Write(); } } }
public void Linq_subexpression_parallel_linq() { using (var db = new FakeMyDbContext()) { var sods = new List <SalesOrderDetail>(); for (int i = 0; i < 10; i++) { sods.Add(new SalesOrderDetail() { SalesOrderId = 71774, ProductId = 905, OrderQty = 4, UnitPrice = 218.454m }); } db.SalesOrderDetails.AddRange(sods); var products = new Product() { ProductId = 905, StandardCost = 199.3757m }; db.Products.Add(products); using (Log.Logger.BeginTimedOperation("Calculating margin with second iteration", "Test")) { _calcLinePriceCounter.Reset(); _calcLineCostCounter.Reset(); _calcMarginCounter.Reset(); var results = db.SalesOrderDetails .Where(sod => sod.SalesOrderId == 71774) .Join(db.Products, sod => sod.ProductId, product => product.ProductId, (sod, product) => new { sod, product }) .ToList() .AsParallel() .Select(li => new { lineTotal = CalculateLinePrice( li.sod.OrderQty, li.sod.UnitPrice, _calcLinePriceCounter), lineCost = CalculateLineCost( li.sod.OrderQty, li.product.StandardCost, _calcLineCostCounter), li }) .Select(lineItem => { return(new { lineItem.li.sod.OrderQty, lineItem.li.sod.UnitPrice, lineItem.li.product.StandardCost, LineTotal = lineItem.lineTotal, LineCost = lineItem.lineCost, Margin = CalculateMargin(lineItem.lineCost, lineItem.lineTotal, _calcMarginCounter) }); }).ToList(); _calcLinePriceCounter.Write(); _calcLineCostCounter.Write(); _calcMarginCounter.Write(); } } }
public void Linq_subexpression_query_syntax() { using (var db = new FakeMyDbContext()) { var sods = new List <SalesOrderDetail>(); for (int i = 0; i < 10; i++) { sods.Add(new SalesOrderDetail() { SalesOrderId = 71774, ProductId = 905, OrderQty = 4, UnitPrice = 218.454m }); } db.SalesOrderDetails.AddRange(sods); var products = new Product() { ProductId = 905, StandardCost = 199.3757m }; db.Products.Add(products); using (Log.Logger.BeginTimedOperation("Calculating margin with second iteration", "Test")) { _calcLinePriceCounter.Reset(); _calcLineCostCounter.Reset(); _calcMarginCounter.Reset(); var results = (from li in (from sod in db.SalesOrderDetails where sod.SalesOrderId == 71774 join product in db.Products on sod.ProductId equals product.ProductId select new { sod, product }) let lineTotal = CalculateLinePrice( li.sod.OrderQty, li.sod.UnitPrice, _calcLinePriceCounter) let lineCost = CalculateLineCost( li.sod.OrderQty, li.product.StandardCost, _calcLineCostCounter) select new { li.sod.OrderQty, li.sod.UnitPrice, li.product.StandardCost, LineTotal = lineTotal, LineCost = lineCost, Margin = CalculateMargin( lineCost, lineTotal, _calcMarginCounter) }).ToList(); //_output.WriteLine(results.ToJson()); _calcLinePriceCounter.Write(); _calcLineCostCounter.Write(); _calcMarginCounter.Write(); } } }
public void Iterate_results_twice_margin() { using (var db = new FakeMyDbContext()) { var sods = new List<SalesOrderDetail>(); for (int i = 0; i < 10; i++) { sods.Add(new SalesOrderDetail() { SalesOrderId = 71774, ProductId = 905, OrderQty = 4, UnitPrice = 218.454m }); } db.SalesOrderDetails.AddRange(sods); var products = new Product() { ProductId = 905, StandardCost = 199.3757m }; db.Products.Add(products); using (Log.Logger.BeginTimedOperation("Calculating margin with second iteration", "Test")) { _calcLinePriceCounter.Reset(); _calcLineCostCounter.Reset(); _calcMarginCounter.Reset(); var results = db.SalesOrderDetails .Where(sod => sod.SalesOrderId == 71774) .Join(db.Products, sod => sod.ProductId, product => product.ProductId, (sod, product) => new { sod, product }) .ToList() .Select(li => new { li.sod.OrderQty, li.sod.UnitPrice, li.product.StandardCost, LineTotal = CalculateLinePrice( li.sod.OrderQty, li.sod.UnitPrice, _calcLinePriceCounter), LineCost = CalculateLineCost( li.sod.OrderQty, li.product.StandardCost, _calcLineCostCounter) }).ToList(); var resultsAgain = results .Select(li => new { li.OrderQty, li.UnitPrice, li.StandardCost, li.LineTotal, li.LineCost, Margin = CalculateMargin( li.LineCost, li.LineTotal, _calcMarginCounter) }).ToList(); _calcLinePriceCounter.Write(); _calcLineCostCounter.Write(); _calcMarginCounter.Write(); } } }
public void Multliple_method_calls_linetotal() { using (var db = new FakeMyDbContext()) { var sods = new List<SalesOrderDetail>(); for (int i = 0; i < 10; i++) { sods.Add(new SalesOrderDetail() { SalesOrderId = 71774, ProductId = 905, OrderQty = 4, UnitPrice = 218.454m }); } db.SalesOrderDetails.AddRange(sods); using (Log.Logger.BeginTimedOperation("Calculating quick total", "Test")) { _calcLinePriceCounter.Reset(); var results = db.SalesOrderDetails .Where(sod => sod.SalesOrderId == 71774) .ToList() .Select(sod => new { sod.OrderQty, sod.UnitPrice, LineTotal = CalculateLinePrice( sod.OrderQty, sod.UnitPrice, _calcLinePriceCounter) }).ToList(); _calcLinePriceCounter.Write(); } } }
public void Linq_subexpression_query_syntax() { using (var db = new FakeMyDbContext()) { var sods = new List<SalesOrderDetail>(); for (int i = 0; i < 10; i++) { sods.Add(new SalesOrderDetail() { SalesOrderId = 71774, ProductId = 905, OrderQty = 4, UnitPrice = 218.454m }); } db.SalesOrderDetails.AddRange(sods); var products = new Product() { ProductId = 905, StandardCost = 199.3757m }; db.Products.Add(products); using (Log.Logger.BeginTimedOperation("Calculating margin with second iteration", "Test")) { _calcLinePriceCounter.Reset(); _calcLineCostCounter.Reset(); _calcMarginCounter.Reset(); var results = (from li in (from sod in db.SalesOrderDetails where sod.SalesOrderId == 71774 join product in db.Products on sod.ProductId equals product.ProductId select new { sod, product }) let lineTotal = CalculateLinePrice( li.sod.OrderQty, li.sod.UnitPrice, _calcLinePriceCounter) let lineCost = CalculateLineCost( li.sod.OrderQty, li.product.StandardCost, _calcLineCostCounter) select new { li.sod.OrderQty, li.sod.UnitPrice, li.product.StandardCost, LineTotal = lineTotal, LineCost = lineCost, Margin = CalculateMargin( lineCost, lineTotal, _calcMarginCounter) }).ToList(); //_output.WriteLine(results.ToJson()); _calcLinePriceCounter.Write(); _calcLineCostCounter.Write(); _calcMarginCounter.Write(); } } }