ASP.NET-MVC4 - 用于在数据库中选择最大“供应商”的Linq查询

时间:2017-08-09 14:49:32

标签: c# linq asp.net-mvc-4

我在数据库中有2个表

供应商表:SupplierID - SupplierName

产品表:ProductID - ProductName - UnitsInStock - SupplierID

如何选择具有最大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);
    }

这是我的数据图片

我需要获得supplierID 345,因为我们在商店中有20个单位属于他,其中5 + 3 + 0 = 8个单位的其他供应商

1 个答案:

答案 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;}
}