【问题标题】:ASP.NET-MVC4 - Linq query to select largest "supplier" in a databaseASP.NET-MVC4 - Linq 查询以选择数据库中最大的“供应商”
【发布时间】:2018-01-17 12:59:31
【问题描述】:

我在一个数据库中有 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);
    }

这是我的数据图片

我需要获取供应商 ID 345,因为我们在商店中有 20 件属于他,这比其他供应商的 5 + 3 + 0 = 8 件要多

【问题讨论】:

  • 你有一些代码可以给我们看吗?
  • 查看您的表架构,您不能。您的产品表需要一个 SupplierID 外键。
  • 哪个表有哪个供应商供应哪个产品的数据?你想得到什么查询结果?你能分享示例输入数据和预期输出吗?
  • 我用代码编辑了问题并添加了supplierId外键。
  • db1.Products.OrderByDescending(e => e.UnitsOnOrder).Take(1).Select(e => e.Supplier).FirstOrDefault();?难道你不做这样的事吗?

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


【解决方案1】:

如果您只想找到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;}
}

【讨论】:

  • .toString 给了我一个错误,我刚刚删除它并完美运行谢谢
  • 我刚刚更新了我的答案,以显示如果您有 2 个供应商的库存数量相同,并且该数量是最大的。
  • 如果我们有 2 家供应商的库存单位是合格的,会发生什么?它会产生错误吗?
  • 如果您使用的是我的第一个解决方案 - 不,它不会产生错误。它只会返回列表中具有最多单位数量的 first 供应商有库存.. 因此,如果有 2 个供应商的库存数量相等.. 第一个解决方案只会返回它在列表中找到的第一个供应商.. 我的第二个解决方案包括 所有 个供应商有相同数量的unitsinstock,而这个数字恰好是最大的
  • 是的,我只是尝试制作这个场景,没有发生错误,它返回了您提到的第一个供应商。谢谢
猜你喜欢
  • 1970-01-01
  • 2016-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多