你可以很容易地编写自己的扩展方法:
public static IEnumerable<IEnumerable<T>> FindSequences<T>(this IEnumerable<T> sequence, Predicate<T> selector, int size)
{
List<T> curSequence = new List<T>();
foreach (T item in sequence)
{
// Check if this item matches the condition
if (selector(item))
{
// It does, so store it
curSequence.Add(item);
// Check if the list size has met the desired size
if (curSequence.Count == size)
{
// It did, so yield that list, and reset
yield return curSequence;
curSequence = new List<T>();
}
}
else
{
// No match, so reset the list
curSequence = new List<T>();
}
}
}
现在你可以说:
var groupsOfFive = entities.OrderBy(x => x.ApplicationTime)
.FindSequences(x => x.ThicknessMicrons == null, 5);
注意这将返回长度5.您的所有子序列可以测试像这样的存在:
bool isFiveSubsequence = groupsOfFive.Any();
另一个重要的注意事项是,如果您有9个连续匹配,则只会找到一个子序列。
不错的工作。我会尽快尝试。 – Hosea146