을 내가 조립 한에 가지 누락 삽입하는 확장 메서드를 LINQ는 - 투 - 객체 범용 순서 :
class EmptyGrouping<K, E> : IGrouping<K, E> {
public K Key { get; set; }
public IEnumerator<E> GetEnumerator() {
return Enumerable.Empty<E>().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return this.GetEnumerator();
}
}
,369 :
public static IEnumerable<T> InsertMissing<T, U>(this IEnumerable<T> source,
Func<T, U> key, Func<U, U> increment, Func<U, T> create)
{
bool first = true;
U last = default(U);
foreach (var ig in source)
{
U current = key(ig);
if (first)
{
first = false;
last = current;
yield return ig;
continue;
}
while (!(last = increment(last)).Equals(current))
{
yield return create(last);
}
yield return ig;
}
}
는 또한 IGrouping
의 사용자 지정 구현을해야합니다
그럼 당신은이 호출을 따르 orderby
후 쿼리를 종료해야합니다, 다음 select
이후에 넣어 :
var allGroups = query.InsertMissing(
// Key selector
g => g.Key,
// Get next desired key from current key
d => d.AddDays(-1),
// Create item for missing key
d => new EmptyGrouping<DateTime,YourAdjustmentType>{ Key = d });
열쇠를 주문하지 않을 경우이 가 훙 분하다거나 경우 그 중 하나가 올바른 위치에 있지 않습니다 (예 : 귀하의 경우에는 자정에 없습니다).
이것은 키 목록을 생성하기 위해 최소/최대 값을 결정하기 위해 원본 소스에서 여러 쿼리를 수행 할 필요가 없다는 이점이 있습니다. 그런 다음 데이터를 조인하고 가져 오기위한 추가 쿼리가 생성됩니다.
좋습니다! 쿼리 확장 프로그램을 좋아해야합니다. –