簡體   English   中英

按屬性過濾對象列表,它是一個整數列表

[英]Filter Object List by Property which is a integer list

我有一個像下面這樣的課程

 Class MyClass {

  string Name ;

  List<int>  scoreList;  //this contains integer values     

   }

我有一個代碼來填充數據庫中上述類類型的對象列表

List<MyClass> newList = //fill from db

我有一個單獨的整數列表,其中包含一些數字

List<int> filterList = new filterList();

現在我想將對象列表(newList)過濾為filterList值中屬性scoreList的任何值

我寫了如下代碼,但似乎給出了錯誤的結果

 newList = newList .Where(x => x.scoreList.All(s => filterList .Contains(s))).ToList();

您能幫我用c#編寫Linq查詢嗎?

newList = newList.Where(s=>s.ScoreList.Intersect(filterList).Any()).ToList();

如果要從newList的所有項目中,一個或多個scoreList值出現在filterList中,則應這樣做:

            newList = newList.Where(x => x.scoreList.Any(s => filterList.Contains(s))).ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM