2016-12-27 131 views
0

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

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,但是我还没有找到解决方案。

+0

你能告诉我们什么是错的吗?你期望什么,你会得到什么? – Sefe

回答

0

这做得更好使用的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; } 
    } 
} 
0

你期望的结果是缺少的,但我可以告诉你,你现在正在做正确的是不会有很大的裨益。

简而言之,您目前要求数据库返回所有匹配所有产品的产品的所有产品,这基本上会导致您获得所有产品(如果数据库没有超时)。因此,我们可以简化您的查询到这一点:

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

从这个选择,那么你要选择产品的名称,类别和供应商的独特名单。这里的主要问题是:你想要一个独特的组合列表,还是三个属性之一需要是唯一的?假设第一,得到你的结果,最简单的方法是这样的:

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(); 

现在都独特的产品名称/分类/供应商组合的列表。