开发者

nhibernate filter a list with another list of object

I'm working on a multi f开发者_高级运维acet engine.

I've 2 types of Class :

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

and

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

I've a List of both class.

  • List < ResultProduct > contains the result of my search.
  • List < Brand > contains the list of the brand.

My objective is to remove all the Brand that are no more in the ResultProduct. (with the other criteria).

How can I do that ?

Edit :

Thanks pektov for your answer. I want to remove all brands which doesn't have products.

I found another solution which work.

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

I think your solution will result in better performance, what do you think ?


I'm not sure i understand the question exactly, im assuming you want to remove all items in the list of brands for witch there are no items in resultProducts with that brandId, without taking into account the List of products in the brand class

If that is the case, you can use RemoveAll method like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜