罗斯文数据库 Linq 查询

Northwind Database Linq Query

我是 Linq 的新手,我正在尝试使用 northwind 数据库编写查询,该数据库应该 return 所有在同一类别中拥有两个或更多产品的供应商。

var test1 =
(from p in Products
join pi in Products on p.CategoryID equals pi.CategoryID
join pf in Products on p.SupplierID equals pf.SupplierID
where p.ProductID != pi.ProductID
select new 
{p.ProductName, p.CategoryID, p.SupplierID}).ToList().Distinct();


test1.Dump();

这是我最后一次尝试,但没有成功。我有点听天由命,因为几个小时以来我一直在努力解决这个问题,但它仍然无法完成预期的工作。也许我完全弄错了?

我的方法是必须有两个或更多具有相同 SupplierID 和 CategoryID 但 ProductID 不同的列表,但我还没有找到解决方案。

最好使用 GroupBy() 来完成:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Product> Products = new List<Product>() {
                new Product() { ProductName = "ABC", CategoryID = 1, SupplierID = 1},
                new Product() { ProductName = "DEF", CategoryID = 1, SupplierID = 1},
                new Product() { ProductName = "GHI", CategoryID = 1, SupplierID = 3},
                new Product() { ProductName = "JKL", CategoryID = 1, SupplierID = 3},
                new Product() { ProductName = "MNO", CategoryID = 2, SupplierID = 1},
                new Product() { ProductName = "PQR", CategoryID = 3, SupplierID = 1},
                new Product() { ProductName = "STU", CategoryID = 4, SupplierID = 1},
                new Product() { ProductName = "VWX", CategoryID = 4, SupplierID = 1},
                new Product() { ProductName = "YZ1", CategoryID = 4, SupplierID = 1},
                new Product() { ProductName = "234", CategoryID = 5, SupplierID = 1}
            };



            var test1 = Products.GroupBy(x => new { supplier = x.SupplierID, category = x.CategoryID })
                .Where(x => x.Count() >= 2).Select(y => y.Select(z => new { name = z.ProductName, supplier = y.Key.supplier, category = y.Key.category })).SelectMany(x => x).ToList();

            foreach (var item in test1)
            {
                Console.WriteLine("Name = '{0}', Supplier = '{1}', Category = '{2}'", item.name, item.supplier, item.category);
            }
            Console.ReadLine();
        }

    }
    public class Product
    {
        public string ProductName { get; set; }
        public int CategoryID { get; set; }
        public int SupplierID { get; set; }
    }
}

没有你想要的结果,但我可以告诉你,你现在做的事情不会有太大的好处。

简而言之,您当前要求数据库 return all products that match all products that match all products,如果数据库没有超时,这基本上会导致您获得所有产品。因此,我们可以将您的查询简化为:

var test1 =
(from p in Products
select new 
{p.ProductName, p.CategoryID, p.SupplierID}).ToList().Distinct();

从这个 selection,您然后想要 select 产品名称、类别和供应商的唯一列表。这里的主要问题是:您想要一个唯一的组合列表,还是三个属性之一需要是唯一的?假设第一个,获得结果的最简单方法如下:

public class ProductResult  : IEquatable<ProductResult> // we have to tell C# compiler how to check if the objects are different from one another
{
   public string Name { get; set; }
   public string Category { get; set; }
   public string Supplier { get; set; }

   public bool Equals(ProductResultother)
            {
                if (other == null)
                    return false;

                return (Category == other.Category) 
                       && Supplier  == other.Supplier)
                       && Name  == other.Name ); // optionally do an .Equals() to make this case-insensitive (check for nulls first if you do this!)
            }
}

那么你可以这样做:

var test1 = (from p in Products
select new ProductResult()
{
   Name = p.ProductName,
   Category = p.CategoryId,
   Supplier = p.SupplierID,
}).Distinct();

您现在拥有所有唯一产品名称/类别/供应商组合的列表。