2015-03-02 98 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; 
    } 
} 
相关问题