2013-07-12 39 views
8

我有一個字符串列表,我需要檢查列表中是否存在特定項目(而不是一個項目)。檢查列表中是否存在以下項目<T>

List<string> strings = new List<string>() {"one","two","three","four","five" }; 

我需要知道「one」和「three」是否在該列表中。一個linq查詢有可能嗎?

感謝您的幫助!

回答

19
var valuesToCheck = new[] {"one", "three"}; 
bool isAllInList = valuesToCheck.All(s => strings.Contains(s)); 
+1

感謝您的幫助! – Dilshod

+1

@Dilshod沒有Linq,但也很容易: 'var valuesToCheck = new HashSet {「one」,「three」,}; bool areAllInList = valuesToCheck.IsSubsetOf(strings);' –

3
var findMe = new List<string>() { "one", "three"}; 
List<string> strings = new List<string>() { "one", "two", "three", "four", "five" }; 

var result = findMe.All(f => strings.Any(s => f == s));