2017-08-09 100 views
0

我有2個數據庫中的表ASP.NET-MVC4 - Linq查詢數據庫中的

供應商表中選擇最大的 「供應商」:供應商ID - SupplierName

產品表:產品ID - 產品名稱 - 庫存量 - 供應商ID

我如何選擇具有最大UnitsInStock的供應商?

這裏的代碼,我

private storeDBEntities2 db1 = new storeDBEntities2(); 
    public ActionResult Index() 
    { 
     var product = db1.Products.Where(e => e.UnitsInStock == 0); 
     var largestSupplier = db1.Products.GroupBy(e => e.SupplierID); 
     Product minimord = db1.Products.OrderBy(e => e.UnitsOnOrder).FirstOrDefault(); 

     var supplier = // this is the query i am struggling with 

     AllModelsProduct all = new AllModelsProduct { Iproduct = product.ToList(), product = new Product(),largestSupplierOfTheStore = supplier,minimumOrders = minimord }; 
     return View(all); 
    } 

這裏是我的數據的圖片

我需要得到供應商ID 345,因爲我們有20個單位的屬於他的店是更大比其他供應商有5 + 3 + 0 = 8個單位

+0

你有一些代碼,你能告訴我們嗎? – Dido

+0

看着你的表架構,你不能。您的產品表需要一個SupplierID外鍵。 –

+0

哪個表格有哪些供應商提供哪些產品的數據?你試圖得到結果的是什麼查詢?你能分享樣本輸入數據和預期輸出嗎? –

回答

2

如果你正在尋找的是找到最大數量的供應商UnitsInStock然後這應該做的伎倆。

我已創建dotNetFiddle供您觀察。

但在這裏它是無論如何:

using System; 
using System.Collections.Generic; 
using System.Linq; 

public class Program 
{ 
    public static void Main() 
    { 
     List<Supply> lstSuppliers = new List<Supply>(); 
     Supply supply1 = new Supply() { ID = 1, SupplierName = "Supplier One"}; 
     Supply supply2 = new Supply() { ID = 2, SupplierName = "Supplier Two"}; 

     lstSuppliers.Add(supply1); 
     lstSuppliers.Add(supply2); 

     Product product1 = new Product() {ID = 1, UnitsInStock = 3, SupplierID = 1}; 
     Product product2 = new Product() {ID = 2, UnitsInStock = 3, SupplierID = 2}; 
     Product product3 = new Product() {ID = 3, UnitsInStock = 5, SupplierID = 1}; 

     List<Product> lstAllProducts = new List<Product>(); 
     lstAllProducts.Add(product1); 
     lstAllProducts.Add(product2); 
     lstAllProducts.Add(product3); 

     var findSupplierId = lstAllProducts.GroupBy(x => x.SupplierID).Select(x => new{ Supplier = x.Key.ToString(), Count = x.Sum(g => g.UnitsInStock)}).OrderByDescending(x => x.Count).First().Supplier; 

     Console.WriteLine(findSupplierId); 



     Console.WriteLine(lstSuppliers.Single(x => x.ID.ToString() == findSupplierId).SupplierName); 

    } 
} 

public class Supply{ 
    public int ID {get;set;} 
    public string SupplierName {get;set;} 
} 

public class Product{ 
    public int ID {get;set;} 
    public int UnitsInStock {get;set;} 
    public int SupplierID {get;set;} 
} 

這裏使用了GroupBy,與創建匿名類來獲得期望的結果一起。

讓我知道這是否有幫助!

更新 - 顯示,如果多個供應商的庫存有相同的單位

using System; 
using System.Collections.Generic; 
using System.Linq; 

public class Program 
{ 
    public static void Main() 
    { 
     List<Supply> lstSuppliers = new List<Supply>(); 
     Supply supply1 = new Supply() { ID = 1, SupplierName = "Supplier One"}; 
     Supply supply2 = new Supply() { ID = 2, SupplierName = "Supplier Two"}; 
     Supply supply3 = new Supply() { ID = 3, SupplierName = "Supplier Three"}; 

     lstSuppliers.Add(supply1); 
     lstSuppliers.Add(supply2); 
     lstSuppliers.Add(supply3); 

     Product product1 = new Product() {ID = 1, UnitsInStock = 3, SupplierID = 1}; 
     Product product2 = new Product() {ID = 2, UnitsInStock = 3, SupplierID = 2}; 
     Product product3 = new Product() {ID = 3, UnitsInStock = 5, SupplierID = 1}; 
     Product product4 = new Product() {ID = 4, UnitsInStock = 8, SupplierID = 3}; 

     List<Product> lstAllProducts = new List<Product>(); 
     lstAllProducts.Add(product1); 
     lstAllProducts.Add(product2); 
     lstAllProducts.Add(product3); 
     lstAllProducts.Add(product4); 

     // finds largest supplier 
     //var findSupplierId = lstAllProducts.GroupBy(x => x.SupplierID).Select(x => new{ Supplier = x.Key.ToString(), Count = x.Sum(g => g.UnitsInStock)}).OrderByDescending(x => x.Count).First().Supplier; 
     //Console.WriteLine(lstSuppliers.Single(x => x.ID.ToString() == findSupplierId).SupplierName); 

     // What if there are multiple suppliers with the same number of units in stock? 

     // first - we have to find the largest number of units in stock 
     var findLargestNumberUIS = lstAllProducts.GroupBy(x => x.SupplierID).Select(x => new{ Supplier = x.Key.ToString(), Count = x.Sum(g => g.UnitsInStock)}).Max(x => x.Count); // 8 

     // second - gather a list of suppliers where their units in stock == findLargestNumberUIS 
     var lstOfLargestSuppliers = lstAllProducts.GroupBy(x => x.SupplierID).Select(x => new{ Supplier = x.Key.ToString(), Count = x.Sum(g => g.UnitsInStock)}).Where(x => x.Count == findLargestNumberUIS).ToList(); 

     // third - loop through lstOfLargestSuppliers to get all suppliers that have the same amount of units in stock which happen to be the largest 
     foreach(var item in lstOfLargestSuppliers){ 
      var supplier = lstSuppliers.Single(x => x.ID.ToString() == item.Supplier).SupplierName; 
      Console.WriteLine(supplier); // print the supplier names to console 

      // Output - Supplier One 
      //   Supplier Three 
     } 



    } 
} 

public class Supply{ 
    public int ID {get;set;} 
    public string SupplierName {get;set;} 
} 

public class Product{ 
    public int ID {get;set;} 
    public int UnitsInStock {get;set;} 
    public int SupplierID {get;set;} 
} 
+0

.toString給了我一個錯誤,我突然刪除它,並完美地工作謝謝 –

+0

我剛剛更新了我的答案,以顯示場景,如果您有2個供應商的庫存量相同,並且該數量最大。 –

+0

如果我們有2個質量單位的供應商,會發生什麼?它會產生一個錯誤? –