nhibernate 用另一个对象列表过滤一个列表

发布于 2024-11-10 01:11:59 字数 797 浏览 14 评论 0原文

我正在研究多方面引擎。

我有两种类型的课程:

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; }
[...]

我有两个课程的列表。

  • 列表<结果产品>包含我的搜索结果。
  • 列表<品牌>包含品牌列表。

我的目标是删除 ResultProduct 中不再存在的所有品牌。 (与其他标准)。

我怎样才能做到这一点?

编辑:

感谢pektov的回答。 我想删除所有没有产品的品牌。

我找到了另一个有效的解决方案。

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

我认为你的解决方案会带来更好的性能,你觉得怎么样?

I'm working on a multi facet 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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹏半生如梦愿梦如真 2024-11-17 01:11:59

我不确定我是否完全理解这个问题,我假设您想删除品牌列表中的所有项目,因为 resultProducts 中没有具有该品牌 ID 的项目,而不考虑品牌类中的产品列表

如果在这种情况下,您可以像这样使用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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文