From 29af7c91833e82b6ed5e152c8c7146402680e288 Mon Sep 17 00:00:00 2001 From: Nikolay Kochulin Date: Sat, 16 Nov 2019 14:23:14 +0000 Subject: [PATCH] Performance improvement in /status --- JetKarmaBot/Commands/StatusCommand.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/JetKarmaBot/Commands/StatusCommand.cs b/JetKarmaBot/Commands/StatusCommand.cs index 4e1ae7a..33e87d8 100644 --- a/JetKarmaBot/Commands/StatusCommand.cs +++ b/JetKarmaBot/Commands/StatusCommand.cs @@ -40,12 +40,14 @@ namespace JetKarmaBot.Commands var awardsQuery = from award in db.Awards where award.ToId == asker.Id && (award.ChatId == args.Message.Chat.Id || isPrivate) group award by award.AwardTypeId into g - select new { AwardTypeId = g.Key, Amount = g.Sum(x => x.Amount) }; + join awardType in db.AwardTypes + on g.Key equals awardType.AwardTypeId + select new { AwardTypeId = g.Key, AwardTypeSymbol = awardType.Symbol, Amount = g.Sum(x => x.Amount) }; var awardsByType = await awardsQuery.ToListAsync(); response = currentLocale["jetkarmabot.status.listalltext"] + "\n" - + string.Join("\n", await Task.WhenAll( - awardsByType.Select(async a => $" - {(await db.AwardTypes.FindAsync(a.AwardTypeId)).Symbol} {a.Amount}") - )); + + string.Join("\n", + awardsByType.Select(a => $" - {a.AwardTypeSymbol} {a.Amount}") + ); } } @@ -57,7 +59,12 @@ namespace JetKarmaBot.Commands var awardTypeId = await awardTypeIdQuery.FirstAsync(); var awardType = await db.AwardTypes.FindAsync(awardTypeId); - response = string.Format(currentLocale["jetkarmabot.status.listspecifictext"], await db.Awards.Where(x => x.AwardTypeId == awardTypeId && x.ToId == asker.Id && x.ChatId == args.Message.Chat.Id).SumAsync(x => x.Amount), awardType.Symbol); + response = string.Format(currentLocale["jetkarmabot.status.listspecifictext"], + await db.Awards.Where( + x => x.AwardTypeId == awardTypeId + && x.ToId == asker.Id + && x.ChatId == args.Message.Chat.Id) + .SumAsync(x => x.Amount), awardType.Symbol); } await Client.SendTextMessageAsync(