If you are talking about collections in memory where comparisons are simple, you can use something like this (but be careful, collections are repeated several times!):
public static bool Contains<T>(this IEnumerable<T> data, IEnumerable<T> otherData) { var dataLength = data.Count(); var otherDataLength = otherData.Count(); if (dataLength < otherDataLength) return false; return Enumerable.Range(0, dataLength - otherDataLength + 1) .Any(skip => data.Skip(skip).Take(otherDataLength).SequenceEqual(otherData)); }
And use it as follows:
var a1 = new List<int> { 1, 2, 3, 4, 5 }; var a2 = new List<int> { 2, 3, 4}; var a3 = new List<int> { 4, 3, 2}; if (a1.Contains(a2)) {
source share