2016-12-27 130 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(); 

現在都獨特的產品名稱/分類/供應商組合的列表。