정말 의미있는 방식으로 주문 테이블인가?
나는 GroupConsecutive
과 같은 식으로 GroupBy
과 다르게 작동하는 그룹화 연산자가 필요하다고 생각합니다. 그래서 모든 별도의 그룹은 동일한 키가있는 경우 결합되지 않고 별도로 유지됩니다.
는 불행하게도 내 VB는 녹슨 - 투 - 존재하지 않는,하지만 당신은 정신이 변환 할 수 있습니다
이제
public static class X
{
private class Grouping<K, V> : List<V>
{
public K Key { get; set; }
}
public static IEnumerable<IGrouping<TKey, TValue>> GroupConsecutive(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector)
{
Grouping current = null;
foreach (var elem in source)
{
var key = keySelector(elem);
if (current == null || !current.Key.Equals(key))
{
if (current != null)
yield return current;
current = new Grouping { Key = key };
}
current.Add(elem);
}
if (current != null)
yield return current;
}
}
당신이 말할 수 있습니다 :
table.GroupConsecutive(r => r.Value).Where(g => g.Key == null).Max(g => g.Count);
다음 표는 치료 중입니다 IEnumerable
으로,이 모든 메모리에서 발생합니다.
원시 SQL에서 수행하는 것이 더 나을 수도 있습니다.
다른 식별 필드가 있습니까, 아니면 문자 그대로 있습니까? – SteadyEddi