2015-11-23 57 views
1

對象的名單上有兩個簡單的模型:使用LINQ,獲得包含特定屬性值

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的產品的清單。

我已經開始寫東西像下面,但還沒有取得多大進展:

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

'。凡(A => activeProductCategories。包含(a.CategoryId);' –

+0

感謝您的幫助! –

回答

1

您可以使用LINQ Contains()方法

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

謝謝,我正在尋找的東西,我感謝幫助。 –