繁体   English   中英

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

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

我有两个简单的模型:

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

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

我写了一个查询来存储ProductCategory.Id号的列表。

List<long> activeProductCategories

现在,我想编写一个Linq查询,该查询获取具有CategoryId等于activeProductCategories中的任何long的所有产品的列表。

我已经开始写如下内容,但是还没有取得很大的进步:

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

您可以使用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