public IEnumerable <Recommendation> RecommendQuery(RecommendQuery query) { var account = AccountContext.Accounts.FirstOrDefault(s => s.Id == query.AccountId); if (account == null) { return(null); } IQueryable <Account> accountsQuery = AccountContext.Accounts;//.FromSql(""); accountsQuery = AddQueryItems(accountsQuery, query); accountsQuery = accountsQuery.Where(s => s.Sex != account.Sex); var now = SecondEpochConverter.ConvertTo(DateTime.UtcNow); var recommendation = accountsQuery.Select(a => new { Account = a, Premium = ((a.PremiumStart ?? long.MaxValue) <= now && ((a.PremiumFinish ?? 0) >= now)) ? 1000000000 : 0, Status = GetStatusCount(a.Status), Interests = InterestsComp(a.Interests, account.Interests), AgeDif = Math.Abs((account.Birth - a.Birth).TotalSeconds) }) .Where(r => r.Interests > 0) .OrderByDescending(r => r.Premium) .ThenByDescending(r => r.Status) .ThenByDescending(r => r.Interests) .ThenBy(r => r.AgeDif) .ThenBy(r => r.Account.Id) .Select(r => new Recommendation(r.Account)) .Take(query.Limit); return(recommendation); }
private IQueryable <Account> AddPremiumQuery(IQueryable <Account> accounts, QueryItem queryItem) { switch (queryItem.Predicate) { case Predicate.@null: if (queryItem.Value == "1") { return(accounts.Where(a => a.PremiumStart == null)); } if (queryItem.Value == "0") { return(accounts.Where(a => a.PremiumStart != null)); } throw new Exception($"Wrong value {queryItem.Value}"); case Predicate.now: var now = SecondEpochConverter.ConvertTo(DateTime.UtcNow); return(accounts.Where(a => a.PremiumStart != null && a.PremiumFinish != null && a.PremiumStart <= now && a.PremiumFinish >= now)); default: throw new Exception($"Unsupportend predicate {queryItem.Predicate}"); } }