简体   繁体   English

使用LINQ,获取包含特定属性值的对象的列表

[英]Using LINQ, get a list of objects that contain a particular property value

I have two simple models: 我有两个简单的模型:

 public class Product()
        {
           public long CategoryId {get; set;}
           //...etc
        }

 public class ProductCategory()
        {
           public long Id {get; set;}
           //...etc
        }

I've written a query to store a list of ProductCategory.Id numbers. 我写了一个查询来存储ProductCategory.Id号的列表。

List<long> activeProductCategories

Now I would like to write a Linq Query that gets a List of all of the Products that have CategoryId equal to any long in activeProductCategories. 现在,我想编写一个Linq查询,该查询获取具有CategoryId等于activeProductCategories中的任何long的所有产品的列表。

I have started writing something like the following, but have yet to make much progress: 我已经开始写如下内容,但是还没有取得很大的进步:

List<Product> activeProducts = UnitOfWork.ProductRepository.Get().Where(a=>a.CategoryId //... ?

You can use a linq Contains() method 您可以使用linq Contains()方法

List<Product> activeProducts = UnitOfWork.ProductRepository.Get()
    .Where(a => activeProductCategories.Contains(a.CategoryId)).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM