问题描述
|
我正在研究多方面引擎。
我有2种类型的Class:
包含我的搜索结果。
列表<品牌>包含品牌列表。
我的目标是删除ResultProduct中不再存在的所有品牌。 (以及其他条件)。
我怎样才能做到这一点 ?
编辑:
感谢pektov的回答。
我要删除所有没有商品的品牌。
我找到了另一个可行的解决方案。
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; }
[...]
我有两个班级的名单。
列表brands = (from brand in brands
where (from res in resultSearch select res.Brand.IdBrand).Contains(brand.IdBrand)
select brand).ToList<Brand>();
我认为您的解决方案将带来更好的性能,您认为呢?
解决方法
我不确定我是否完全理解这个问题,我假设您要删除女巫的品牌列表中的所有项目,而结果产品中没有该brandId的项目中没有任何项目,而不考虑品牌类中的产品列表
如果是这种情况,可以使用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