8
我有一個字符串列表,我需要檢查列表中是否存在特定項目(而不是一個項目)。檢查列表中是否存在以下項目<T>
List<string> strings = new List<string>() {"one","two","three","four","five" };
我需要知道「one」和「three」是否在該列表中。一個linq查詢有可能嗎?
感謝您的幫助!
我有一個字符串列表,我需要檢查列表中是否存在特定項目(而不是一個項目)。檢查列表中是否存在以下項目<T>
List<string> strings = new List<string>() {"one","two","three","four","five" };
我需要知道「one」和「three」是否在該列表中。一個linq查詢有可能嗎?
感謝您的幫助!
var valuesToCheck = new[] {"one", "three"};
bool isAllInList = valuesToCheck.All(s => strings.Contains(s));
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));
感謝您的幫助! – Dilshod
@Dilshod沒有Linq,但也很容易: 'var valuesToCheck = new HashSet {「one」,「three」,}; bool areAllInList = valuesToCheck.IsSubsetOf(strings);' –