开发者

nhibernate filter a list with another list of object

开发者 https://www.devze.com 2023-03-08 23:56 出处:网络
I\'m working on a multi f开发者_高级运维acet engine. I\'ve 2 types of Class : ResultProduct public int Id { get; set; }

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

精彩评论

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

关注公众号