2015-03-02 157 views
-4

我有一個列表,當我選擇ID並且我想看看其中一個ID是否存在於另一個ID列表中。我怎麼做?lambda查看列表是否包含來自另一個列表的元素

我試着做這樣的事情:

customerViewModel.Suppliers.Select(w => w.SupplierId).Contains(SessionCms.Suppliers.Select(a => a.SupplierId)) 

我sessioncms對象是供應商的名單,我的customerviewmodel.suppliers也是供應商的名單。

(這裏我只寫了一些廢話,使我達到質量標準張貼此問題)

+0

的可能重複[檢查列表包含任何其他名單](http://stackoverflow.com/questions/11092930/check-if-listt-contains-any-of-another-list) – HimBromBeere 2015-03-02 14:52:38

回答

0

你去那裏:

class Program { 
    static void Main(string[] args) { 
     bool contains = thisList.AnyOf(thatList, t => t.Property); 
     Console.WriteLine(contains); 
     Console.ReadKey(true); 
    } 
} 

static class _ { 
    public static bool AnyOf<T, P>(this IEnumerable<T> thisList, IEnumerable<T> thatList, Func<T, P> propertySelector) { 
     foreach(P thisProperty in thisList.Select(propertySelector)) { 
      foreach(P thatProperty in thatList.Select(propertySelector)) { 
       if(thisProperty.Equals(thatProperty)) { 
        return true; 
       } 
      } 
     } 
     return false; 
    } 
} 
相關問題