2011-11-18 71 views
1

我有一個過濾器列表傳遞到一個web服務,我迭代集合並做Linq查詢,然後添加到產品列表,但當我做一個GroupByDistinct()它不會刪除重複項。我正在使用IEnumerable,因爲當您使用Disinct時,它會將其轉換爲IEnumerable。如果你知道如何更好地構造這個函數,並且使我的函數返回一個List<Product>類型,我將不勝感激。不同的()返回列表<>返回重複

這是我在C#代碼:

if (Tab == "All-Items") 
{ 
    List<Product> temp = new List<Product>(); 
    List<Product> Products2 = new List<Product>(); 
    foreach (Filter filter in Filters) 
    {       
     List<Product> products = (from p in db.Products where p.Discontinued == false 
            && p.DepartmentId == qDepartment.Id 
            join f in db.Filters on p.Id equals f.ProductId 
            join x in db.ProductImages on p.Id equals x.ProductId 
            where x.Dimension == "180X180" 
            && f.Name == filter.Name /*Filter*/ 
            select new Product 
            { 
             Id = p.Id, 
             Title = p.Title, 
             ShortDescription = p.ShortDescription, 
             Brand = p.Brand, 
             Model = p.Model, 
             Image = x.Path, 
             FriendlyUrl = p.FriendlyUrl, 
             SellPrice = p.SellPrice, 
             DiscountPercentage = p.DiscountPercentage, 
             Votes = p.Votes, 
             TotalRating = p.TotalRating 
            }).ToList<Product>(); 

     foreach (Product p in products) 
     { 
      temp.Add(p); 
     }       

     IEnumerable temp2 = temp.GroupBy(x => x.Id).Distinct(); 
     IEnumerator e = temp.GetEnumerator(); 
     while (e.MoveNext()) { 
      Product c = e.Current as Product; 
      Products2.Add(c); 
     } 
    } 
    pf.Products = Products2;// return type must be List<Product>     
} 
+0

我發現你在代碼的早些時候使用了ToList - 你也可以在你的返回值上使用它。 – sq33G

回答

3

你分配GroupBy結果temp2,但你從來沒有使用它!

IEnumerable temp2 = temp.GroupBy(x => x.Id).Distinct();   
IEnumerator e = temp.GetEnumerator(); // <- why temp and not temp2??? 

但是你根本沒有真正使用GroupBy操作符。您需要進行分組,然後從每個組中選擇一個項目,而不是選擇不同的組。這些組是已經不同

當組由ID,您實際上會返回一個結構類似這樣的

-Key: 1 
    -Product (Id = 1) 
-Key: 2 
    -Product (Id = 2) 
    -Product (Id = 2) 

你需要從每個組檢索項目。假設重複項,你不會在乎你得到哪個項目,所以你可以從每個組中獲取第一項。

var groups = temp.GroupBy(x => x.Id); 
var distinctItems = groups.Select(g => g.First()); 

foreach (var item in distinctItems) 
{ 
    // do stuff with item 
    Products2.Add(item); 
} 
+0

...爲什麼你仍然在調用Distinct? – sq33G

+0

@ sq33G很好的皮卡;)純粹是因爲我剪切並粘貼了原始代碼並忘記刪除它。現在修復。 –

+0

我試過你的例子,它沒有工作,這部分在這裏Product c = e.Current as Product;在上下文中不存在任何其他建議 – ONYX

4

你需要重寫EqualsGetHashCode按值來比較Product秒。

+0

我從來沒有做過重寫HasCodes你可以做它或給一個例子做什麼。 – ONYX

+0

http://blogs.msdn.com/b/ericlippert/archive/2011/02/28/guidelines-and-rules-for-gethashcode.aspx – SLaks

+0

http://stackoverflow.com/questions/263400/what-is - 最好的算法 - 重寫 - 系統對象 - gethashcode – SLaks

0

SLaks是正確的,您必須重寫Equals()和GetHashCode()方法來完成此操作。

這是我在我寫的小程序中的一個Order對象內做到的。希望這可以幫助!

//overridden Equals() method from the Object class, determines equality based on order number 
    public override bool Equals(object obj) 
    { 
     bool equal; 
     if (this.GetType() != obj.GetType()) 
      equal = false; 
     else 
     { 
      Order temp = (Order)obj; 
      if(OrderNumber == temp.OrderNumber) 
       equal = true; 
      else 
       equal = false; 
     } 
     return equal; 
    } 

    //overridden GetHashCode() method from Object class, sets the unquie identifier to OrderNumber 
    public override int GetHashCode() 
    { 
     return OrderNumber; 
    } 
+0

當你說對象類是我的類產品或我的web服務時,我很困惑把它放在哪裏 – ONYX

+0

和我需要改變我的代碼,我提供可以更新我提供的 – ONYX

+0

你想在你的產品類,因爲這是你試圖比較。 – jmshapland