One liner approach of Rolling and Tim is very nice, but he has one little information: b repeated twice.
If this is a problem for you, you can use an iterator based approach. This can be created as an extension method:
public static bool IsContainedWithinInOrder<T>(this IEnumerable<T> values, IEnumerable<T> reference) { using(var iterator = reference.GetEnumerator()) { foreach(var item in values) { do { if(!iterator.MoveNext()) return false; } while(!Equals(iterator.Current, item)); } return true; } }
This will only repeat both sequences once and will generally be easier. You would call it like this:
b.IsContainedWithinInOrder(a);
Please forgive the method name ...
source share