![](/img/trans.png)
[英]Linq - get objects with a list property that does not contain values in another list
[英]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.