using NorthwindRazorAPI.BusinessObject; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; namespace NorthwindRazorAPI.DataLayer.Base { /// <summary> /// Base class for ProductsDataLayer. Do not make changes to this class, /// instead, put additional code in the ProductsDataLayer class /// </summary> internal class ProductsDataLayerBase { // constructor internal ProductsDataLayerBase() { } /// <summary> /// Selects a record by primary key(s) /// </summary> internal static async Task<EF.Products> SelectByPrimaryKeyAsync(int productID) { EF.NorthwindContext context = new EF.NorthwindContext(); return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.ProductID == productID).FirstOrDefaultAsync(); } /// <summary> /// Gets the total number of records in the Products table /// </summary> internal static async Task<int> GetRecordCountAsync() { EF.NorthwindContext context = new EF.NorthwindContext(); return await context.Products.Include(p => p.Supplier).Include(p => p.Category).CountAsync(); } /// <summary> /// Gets the total number of records in the Products table by SupplierID /// </summary> internal static async Task<int> GetRecordCountBySupplierIDAsync(int? supplierID) { EF.NorthwindContext context = new EF.NorthwindContext(); return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).CountAsync(); } /// <summary> /// Gets the total number of records in the Products table by CategoryID /// </summary> internal static async Task<int> GetRecordCountByCategoryIDAsync(int? categoryID) { EF.NorthwindContext context = new EF.NorthwindContext(); return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).CountAsync(); } /// <summary> /// Gets the total number of records in the Products table based on search parameters /// </summary> internal static async Task<int> GetRecordCountDynamicWhereAsync(int? productID, string productName, int? supplierID, int? categoryID, string quantityPerUnit, decimal? unitPrice, Int16? unitsInStock, Int16? unitsOnOrder, Int16? reorderLevel, bool? discontinued) { EF.NorthwindContext context = new EF.NorthwindContext(); int productIDValue = int.MinValue; int supplierIDValue = int.MinValue; int categoryIDValue = int.MinValue; decimal unitPriceValue = decimal.MinValue; Int16 unitsInStockValue = Int16.MinValue; Int16 unitsOnOrderValue = Int16.MinValue; Int16 reorderLevelValue = Int16.MinValue; bool discontinuedValue = false; if (productID != null) productIDValue = productID.Value; if (supplierID != null) supplierIDValue = supplierID.Value; if (categoryID != null) categoryIDValue = categoryID.Value; if (unitPrice != null) unitPriceValue = unitPrice.Value; if (unitsInStock != null) unitsInStockValue = unitsInStock.Value; if (unitsOnOrder != null) unitsOnOrderValue = unitsOnOrder.Value; if (reorderLevel != null) reorderLevelValue = reorderLevel.Value; if (discontinued != null) discontinuedValue = discontinued.Value; return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).CountAsync(); } /// <summary> /// Selects Products records sorted by the sortByExpression and returns records from the startRowIndex with rows (# of rows) /// </summary> internal static async Task<List<EF.Products>> SelectSkipAndTakeAsync(string sortByExpression, int startRowIndex, int rows) { EF.NorthwindContext context = new EF.NorthwindContext(); if (sortByExpression.Contains(" desc")) { switch (sortByExpression) { case "ProductName desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderByDescending(p => p.ProductName).Skip(startRowIndex).Take(rows).ToListAsync(); case "SupplierID desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderByDescending(p => p.SupplierID).Skip(startRowIndex).Take(rows).ToListAsync(); case "CategoryID desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderByDescending(p => p.CategoryID).Skip(startRowIndex).Take(rows).ToListAsync(); case "QuantityPerUnit desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderByDescending(p => p.QuantityPerUnit).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitPrice desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderByDescending(p => p.UnitPrice).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitsInStock desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderByDescending(p => p.UnitsInStock).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitsOnOrder desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderByDescending(p => p.UnitsOnOrder).Skip(startRowIndex).Take(rows).ToListAsync(); case "ReorderLevel desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderByDescending(p => p.ReorderLevel).Skip(startRowIndex).Take(rows).ToListAsync(); case "Discontinued desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderByDescending(p => p.Discontinued).Skip(startRowIndex).Take(rows).ToListAsync(); default: return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderByDescending(p => p.ProductID).Skip(startRowIndex).Take(rows).ToListAsync(); } } else { switch (sortByExpression) { case "ProductName": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderBy(p => p.ProductName).Skip(startRowIndex).Take(rows).ToListAsync(); case "SupplierID": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderBy(p => p.SupplierID).Skip(startRowIndex).Take(rows).ToListAsync(); case "CategoryID": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderBy(p => p.CategoryID).Skip(startRowIndex).Take(rows).ToListAsync(); case "QuantityPerUnit": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderBy(p => p.QuantityPerUnit).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitPrice": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderBy(p => p.UnitPrice).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitsInStock": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderBy(p => p.UnitsInStock).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitsOnOrder": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderBy(p => p.UnitsOnOrder).Skip(startRowIndex).Take(rows).ToListAsync(); case "ReorderLevel": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderBy(p => p.ReorderLevel).Skip(startRowIndex).Take(rows).ToListAsync(); case "Discontinued": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderBy(p => p.Discontinued).Skip(startRowIndex).Take(rows).ToListAsync(); default: return await context.Products.Include(p => p.Supplier).Include(p => p.Category).OrderBy(p => p.ProductID).Skip(startRowIndex).Take(rows).ToListAsync(); } } } /// <summary> /// Selects records by SupplierID as a collection (List) of Products sorted by the sortByExpression. /// </summary> internal static async Task<List<EF.Products>> SelectSkipAndTakeBySupplierIDAsync(string sortByExpression, int startRowIndex, int rows, int? supplierID) { EF.NorthwindContext context = new EF.NorthwindContext(); if (sortByExpression.Contains(" desc")) { switch (sortByExpression) { case "ProductName desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderByDescending(p => p.ProductName).Skip(startRowIndex).Take(rows).ToListAsync(); case "SupplierID desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderByDescending(p => p.SupplierID).Skip(startRowIndex).Take(rows).ToListAsync(); case "CategoryID desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderByDescending(p => p.CategoryID).Skip(startRowIndex).Take(rows).ToListAsync(); case "QuantityPerUnit desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderByDescending(p => p.QuantityPerUnit).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitPrice desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderByDescending(p => p.UnitPrice).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitsInStock desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderByDescending(p => p.UnitsInStock).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitsOnOrder desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderByDescending(p => p.UnitsOnOrder).Skip(startRowIndex).Take(rows).ToListAsync(); case "ReorderLevel desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderByDescending(p => p.ReorderLevel).Skip(startRowIndex).Take(rows).ToListAsync(); case "Discontinued desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderByDescending(p => p.Discontinued).Skip(startRowIndex).Take(rows).ToListAsync(); default: return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderByDescending(p => p.ProductID).Skip(startRowIndex).Take(rows).ToListAsync(); } } else { switch (sortByExpression) { case "ProductName": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderBy(p => p.ProductName).Skip(startRowIndex).Take(rows).ToListAsync(); case "SupplierID": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderBy(p => p.SupplierID).Skip(startRowIndex).Take(rows).ToListAsync(); case "CategoryID": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderBy(p => p.CategoryID).Skip(startRowIndex).Take(rows).ToListAsync(); case "QuantityPerUnit": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderBy(p => p.QuantityPerUnit).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitPrice": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderBy(p => p.UnitPrice).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitsInStock": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderBy(p => p.UnitsInStock).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitsOnOrder": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderBy(p => p.UnitsOnOrder).Skip(startRowIndex).Take(rows).ToListAsync(); case "ReorderLevel": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderBy(p => p.ReorderLevel).Skip(startRowIndex).Take(rows).ToListAsync(); case "Discontinued": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderBy(p => p.Discontinued).Skip(startRowIndex).Take(rows).ToListAsync(); default: return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).OrderBy(p => p.ProductID).Skip(startRowIndex).Take(rows).ToListAsync(); } } } /// <summary> /// Selects records by CategoryID as a collection (List) of Products sorted by the sortByExpression. /// </summary> internal static async Task<List<EF.Products>> SelectSkipAndTakeByCategoryIDAsync(string sortByExpression, int startRowIndex, int rows, int? categoryID) { EF.NorthwindContext context = new EF.NorthwindContext(); if (sortByExpression.Contains(" desc")) { switch (sortByExpression) { case "ProductName desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderByDescending(p => p.ProductName).Skip(startRowIndex).Take(rows).ToListAsync(); case "SupplierID desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderByDescending(p => p.SupplierID).Skip(startRowIndex).Take(rows).ToListAsync(); case "CategoryID desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderByDescending(p => p.CategoryID).Skip(startRowIndex).Take(rows).ToListAsync(); case "QuantityPerUnit desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderByDescending(p => p.QuantityPerUnit).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitPrice desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderByDescending(p => p.UnitPrice).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitsInStock desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderByDescending(p => p.UnitsInStock).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitsOnOrder desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderByDescending(p => p.UnitsOnOrder).Skip(startRowIndex).Take(rows).ToListAsync(); case "ReorderLevel desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderByDescending(p => p.ReorderLevel).Skip(startRowIndex).Take(rows).ToListAsync(); case "Discontinued desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderByDescending(p => p.Discontinued).Skip(startRowIndex).Take(rows).ToListAsync(); default: return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderByDescending(p => p.ProductID).Skip(startRowIndex).Take(rows).ToListAsync(); } } else { switch (sortByExpression) { case "ProductName": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderBy(p => p.ProductName).Skip(startRowIndex).Take(rows).ToListAsync(); case "SupplierID": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderBy(p => p.SupplierID).Skip(startRowIndex).Take(rows).ToListAsync(); case "CategoryID": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderBy(p => p.CategoryID).Skip(startRowIndex).Take(rows).ToListAsync(); case "QuantityPerUnit": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderBy(p => p.QuantityPerUnit).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitPrice": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderBy(p => p.UnitPrice).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitsInStock": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderBy(p => p.UnitsInStock).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitsOnOrder": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderBy(p => p.UnitsOnOrder).Skip(startRowIndex).Take(rows).ToListAsync(); case "ReorderLevel": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderBy(p => p.ReorderLevel).Skip(startRowIndex).Take(rows).ToListAsync(); case "Discontinued": return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderBy(p => p.Discontinued).Skip(startRowIndex).Take(rows).ToListAsync(); default: return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).OrderBy(p => p.ProductID).Skip(startRowIndex).Take(rows).ToListAsync(); } } } /// <summary> /// Selects Products records sorted by the sortByExpression and returns records from the startRowIndex with rows (# of records) based on search parameters /// </summary> internal static async Task<List<EF.Products>> SelectSkipAndTakeDynamicWhereAsync(int? productID, string productName, int? supplierID, int? categoryID, string quantityPerUnit, decimal? unitPrice, Int16? unitsInStock, Int16? unitsOnOrder, Int16? reorderLevel, bool? discontinued, string sortByExpression, int startRowIndex, int rows) { EF.NorthwindContext context = new EF.NorthwindContext(); int productIDValue = int.MinValue; int supplierIDValue = int.MinValue; int categoryIDValue = int.MinValue; decimal unitPriceValue = decimal.MinValue; Int16 unitsInStockValue = Int16.MinValue; Int16 unitsOnOrderValue = Int16.MinValue; Int16 reorderLevelValue = Int16.MinValue; bool discontinuedValue = false; if (productID != null) productIDValue = productID.Value; if (supplierID != null) supplierIDValue = supplierID.Value; if (categoryID != null) categoryIDValue = categoryID.Value; if (unitPrice != null) unitPriceValue = unitPrice.Value; if (unitsInStock != null) unitsInStockValue = unitsInStock.Value; if (unitsOnOrder != null) unitsOnOrderValue = unitsOnOrder.Value; if (reorderLevel != null) reorderLevelValue = reorderLevel.Value; if (discontinued != null) discontinuedValue = discontinued.Value; if (sortByExpression.Contains(" desc")) { switch (sortByExpression) { case "ProductName desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderByDescending(p => p.ProductName).Skip(startRowIndex).Take(rows).ToListAsync(); case "SupplierID desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderByDescending(p => p.SupplierID).Skip(startRowIndex).Take(rows).ToListAsync(); case "CategoryID desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderByDescending(p => p.CategoryID).Skip(startRowIndex).Take(rows).ToListAsync(); case "QuantityPerUnit desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderByDescending(p => p.QuantityPerUnit).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitPrice desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderByDescending(p => p.UnitPrice).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitsInStock desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderByDescending(p => p.UnitsInStock).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitsOnOrder desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderByDescending(p => p.UnitsOnOrder).Skip(startRowIndex).Take(rows).ToListAsync(); case "ReorderLevel desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderByDescending(p => p.ReorderLevel).Skip(startRowIndex).Take(rows).ToListAsync(); case "Discontinued desc": return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderByDescending(p => p.Discontinued).Skip(startRowIndex).Take(rows).ToListAsync(); default: return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderByDescending(p => p.ProductID).Skip(startRowIndex).Take(rows).ToListAsync(); } } else { switch (sortByExpression) { case "ProductName": return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderBy(p => p.ProductName).Skip(startRowIndex).Take(rows).ToListAsync(); case "SupplierID": return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderBy(p => p.SupplierID).Skip(startRowIndex).Take(rows).ToListAsync(); case "CategoryID": return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderBy(p => p.CategoryID).Skip(startRowIndex).Take(rows).ToListAsync(); case "QuantityPerUnit": return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderBy(p => p.QuantityPerUnit).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitPrice": return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderBy(p => p.UnitPrice).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitsInStock": return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderBy(p => p.UnitsInStock).Skip(startRowIndex).Take(rows).ToListAsync(); case "UnitsOnOrder": return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderBy(p => p.UnitsOnOrder).Skip(startRowIndex).Take(rows).ToListAsync(); case "ReorderLevel": return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderBy(p => p.ReorderLevel).Skip(startRowIndex).Take(rows).ToListAsync(); case "Discontinued": return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderBy(p => p.Discontinued).Skip(startRowIndex).Take(rows).ToListAsync(); default: return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).OrderBy(p => p.ProductID).Skip(startRowIndex).Take(rows).ToListAsync(); } } } /// <summary> /// Selects all Products /// </summary> internal static async Task<List<EF.Products>> SelectAllAsync() { EF.NorthwindContext context = new EF.NorthwindContext(); return await context.Products.Include(p => p.Supplier).Include(p => p.Category).ToListAsync(); } /// <summary> /// Selects records based on the passed filters as a collection (List) of Products. /// </summary> internal static async Task<List<EF.Products>> SelectAllDynamicWhereAsync(int? productID, string productName, int? supplierID, int? categoryID, string quantityPerUnit, decimal? unitPrice, Int16? unitsInStock, Int16? unitsOnOrder, Int16? reorderLevel, bool? discontinued) { EF.NorthwindContext context = new EF.NorthwindContext(); int productIDValue = int.MinValue; int supplierIDValue = int.MinValue; int categoryIDValue = int.MinValue; decimal unitPriceValue = decimal.MinValue; Int16 unitsInStockValue = Int16.MinValue; Int16 unitsOnOrderValue = Int16.MinValue; Int16 reorderLevelValue = Int16.MinValue; bool discontinuedValue = false; if (productID != null) productIDValue = productID.Value; if (supplierID != null) supplierIDValue = supplierID.Value; if (categoryID != null) categoryIDValue = categoryID.Value; if (unitPrice != null) unitPriceValue = unitPrice.Value; if (unitsInStock != null) unitsInStockValue = unitsInStock.Value; if (unitsOnOrder != null) unitsOnOrderValue = unitsOnOrder.Value; if (reorderLevel != null) reorderLevelValue = reorderLevel.Value; if (discontinued != null) discontinuedValue = discontinued.Value; return await context.Products.Include(p => p.Supplier).Include(p => p.Category) .Where(p => (productID != null ? p.ProductID == productIDValue : 1 == 1) && (!String.IsNullOrEmpty(productName) ? p.ProductName.Contains(productName) : 1 == 1) && (supplierID != null ? p.SupplierID == supplierIDValue : 1 == 1) && (categoryID != null ? p.CategoryID == categoryIDValue : 1 == 1) && (!String.IsNullOrEmpty(quantityPerUnit) ? p.QuantityPerUnit.Contains(quantityPerUnit) : 1 == 1) && (unitPrice != null ? p.UnitPrice == unitPriceValue : 1 == 1) && (unitsInStock != null ? p.UnitsInStock == unitsInStockValue : 1 == 1) && (unitsOnOrder != null ? p.UnitsOnOrder == unitsOnOrderValue : 1 == 1) && (reorderLevel != null ? p.ReorderLevel == reorderLevelValue : 1 == 1) && (discontinued != null ? p.Discontinued == discontinuedValue : 1 == 1) ).ToListAsync(); } /// <summary> /// Selects all Products by Suppliers, related to column SupplierID /// </summary> internal static async Task<List<EF.Products>> SelectProductsCollectionBySupplierIDAsync(int supplierID) { EF.NorthwindContext context = new EF.NorthwindContext(); return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.SupplierID == supplierID).ToListAsync(); } /// <summary> /// Selects all Products by Categories, related to column CategoryID /// </summary> internal static async Task<List<EF.Products>> SelectProductsCollectionByCategoryIDAsync(int categoryID) { EF.NorthwindContext context = new EF.NorthwindContext(); return await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.CategoryID == categoryID).ToListAsync(); } /// <summary> /// Selects ProductID and ProductName columns for use with a DropDownList web control /// </summary> internal static async Task<List<EF.Products>> SelectProductsDropDownListDataAsync() { EF.NorthwindContext context = new EF.NorthwindContext(); return await (from p in context.Products.Include(p => p.Supplier).Include(p => p.Category) select new EF.Products { ProductID = p.ProductID, ProductName = p.ProductName }).ToListAsync(); } /// <summary> /// Inserts a record /// </summary> internal static async Task<int> InsertAsync(Products objProducts) { EF.NorthwindContext context = new EF.NorthwindContext(); EF.Products efProducts = new EF.Products(); efProducts.ProductName = objProducts.ProductName; efProducts.SupplierID = objProducts.SupplierID; efProducts.CategoryID = objProducts.CategoryID; efProducts.QuantityPerUnit = objProducts.QuantityPerUnit; efProducts.UnitPrice = objProducts.UnitPrice; efProducts.UnitsInStock = objProducts.UnitsInStock; efProducts.UnitsOnOrder = objProducts.UnitsOnOrder; efProducts.ReorderLevel = objProducts.ReorderLevel; efProducts.Discontinued = objProducts.Discontinued; context.Products.Add(efProducts); await context.SaveChangesAsync(); return efProducts.ProductID; } /// <summary> /// Updates a record /// </summary> internal static async Task UpdateAsync(Products objProducts) { EF.NorthwindContext context = new EF.NorthwindContext(); EF.Products efProducts = await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.ProductID == objProducts.ProductID).FirstOrDefaultAsync(); if (efProducts != null) { efProducts.ProductName = objProducts.ProductName; efProducts.SupplierID = objProducts.SupplierID; efProducts.CategoryID = objProducts.CategoryID; efProducts.QuantityPerUnit = objProducts.QuantityPerUnit; efProducts.UnitPrice = objProducts.UnitPrice; efProducts.UnitsInStock = objProducts.UnitsInStock; efProducts.UnitsOnOrder = objProducts.UnitsOnOrder; efProducts.ReorderLevel = objProducts.ReorderLevel; efProducts.Discontinued = objProducts.Discontinued; await context.SaveChangesAsync(); } } /// <summary> /// Deletes a record based on primary key(s) /// </summary> internal static async Task DeleteAsync(int productID) { EF.NorthwindContext context = new EF.NorthwindContext(); var objProducts = await context.Products.Include(p => p.Supplier).Include(p => p.Category).Where(p => p.ProductID == productID).FirstOrDefaultAsync(); if (objProducts != null) { context.Products.Remove(objProducts); await context.SaveChangesAsync(); } } } }