2017-08-09 102 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个质量单位的供应商,会发生什么?它会产生一个错误? –