Zakładając, że mam strukturę jak poniżej:
public class IdentityCard
{
...
public string OwnerId { get; set; }
public ApplicationUser Owner { get; set; }
public ICollection<AccountingRecord> AccountingRecords { get; set; }
}
public class AccountingRecord
{
...
public int IdentityCardId { get; set; }
public IdentityCard IdentityCard { get; set; }
[Required]
public DateTime Timestamp { get; set; }
}
chciałbym odfiltrować wyniki po userze, offsecie czasowym, a następnie pogrupować je po godzinie z właściwości Timestamp. Mam już coś takiego jak poniżej, ale nie potrafię poprawnie zapisać grupowania przy zależnościach 1:n.
var accountingIdentityCards = _context.Users
.Where(r => r.Id == userId)
.Include(s => s.IdentityCards)
.ThenInclude(r => r.AccountingRecords)
.Where(a => a.IdentityCards.Any(x => x.AccountingRecords.Any(s => s.Timestamp >= DateTime.Today.AddDays(periodOffset))) && a.IdentityCards.Any(x => x.AccountingRecords.Any(s => s.Timestamp <= DateTime.Today.AddDays(1 + periodOffset))))
.GroupBy(x => x.IdentityCards.AccountingRecords.Timestamp.Hour, (key, group) => new Tuple<string, int>(group.First().Timestamp.Hour.ToString(), group.Count()))
.ToList();
W jaki sposób poprawnie to zapisać, żeby podało liczbę rekordów w danej godzinie?