2011-05-26 95 views
1

我正在研究多方面引擎。nhibernate使用另一個對象列表篩選列表

我有2種類:

ResultProduct 
public int Id { get; set; } 
public int Name { get; set; } 
public int Brand { get; set; } 
[...] 

Brand 
public int Id { get; set; } 
public int Name { get; set; } 
public IList<Product> Product { get; set; } 
[...] 

我有兩個類的列表。

  • List < ResultProduct>包含我的搜索結果。
  • 列表<品牌>包含品牌列表。

我的目標是刪除ResultProduct中不再包含的所有品牌。 (與其他標準)。

我該怎麼做?

編輯:

感謝pektov你的答案。 我想刪除所有沒有產品的品牌。

我發現了另一個工作的解決方案。

brands = (from brand in brands 
    where (from res in resultSearch select res.Brand.IdBrand).Contains(brand.IdBrand) 
    select brand).ToList<Brand>(); 

我認爲您的解決方案會帶來更好的性能,您怎麼看?

回答

1

我不確定我是否完全理解問題,即時假設您想要移除品牌列表中的所有商品,以便女士在resultProducts中沒有該品牌標識中的項目,而不考慮產品列表中的商品列表品牌類

如果是這樣的話,你可以使用RemoveAll方法是這樣的:

List<ResultProducts> products; 
List<Brands> brands; 

brands.RemoveAll(x=> !products.Exists(y=>y.brand == x.Id)); //returns only brands that don't appear in the products list 
+0

我找到了另一種解決辦法,我編輯的問題。什麼解決方案最適合你? – 2011-05-26 14:06:35

+0

我不認爲應該有任何重大區別。 – 2011-05-26 14:44:09

相關問題