public List<User> RetrieveNewUsers()
        {
            using (CollectContext context = new CollectContext())
            {
                try
                {
                    DateTime hierMemeHeure = DateTime.Now.AddDays(-1d);

                    var nouveauxInscrits = from u in context.Users
                                           where u.CreationDate > hierMemeHeure
                                               //&& !u.IsCanal // [12/05/2014] temps 4 : on récupère désormais tous les inscrits optins
                                           && u.IsOffreGroupCanal
                                           orderby u.CreationDate descending
                                           select u;

                    return nouveauxInscrits.ToList();

                }
                catch (Exception e)
                {
                    Program.Log("Probleme base de données : " + e.Message + " (" + e.StackTrace + ")");
                    return new List<User>();
                }
            }
        }
        public StdResult<AnswerChoice> SetAnswer(User u, int questionNumber, int answerChosen)
        {
            using (CollectContext context = new CollectContext())
            {
                if(!context.Users.Any(user => user.Id == u.Id))
                    return StdResult<AnswerChoice>.BadResult("User introuvable en base.");

                var query = context.AnswerChoices.Where(answerChoice => answerChoice.User.Id == u.Id && answerChoice.QuestionNumber == questionNumber);
                bool alreadyAnswered = query.Any();
                AnswerChoice ac = null;
                if (query.Any())
                {
                    ac = query.FirstOrDefault();
                    ac.AnswerChosen = answerChosen;
                }
                else
                {
                    ac = new AnswerChoice
                    {
                        UserId = u.Id,
                        AnswerChosen = answerChosen,
                        QuestionNumber = questionNumber
                    };
                    context.AnswerChoices.Add(ac);

                    context.SaveChanges();
                    u.AnswerChoices.Add(ac);
                }

                context.SaveChanges();
                return StdResult<AnswerChoice>.OkResultInstance(ac);
            }
        }
        public StdResult<InstantGagnant> WinInstantGagnant(User MainUser)
        {
            using (CollectContext context = new CollectContext())
            {
                DateTime now = DateTime.Now;
                var query = from ig in context.InstantsGagnants
                            where now > ig.StartDateTime
                            && !ig.Won
                            orderby ig.StartDateTime
                            select ig;
                if (query.Any<InstantGagnant>())
                {
                    InstantGagnant ig = query.First<InstantGagnant>();

                    ig.UserId = MainUser.Id;
                    ig.Won = true;
                    ig.WonDate = DateTime.Now;
                    MainUser.InstantsGagnantWon.Add(ig);
                    context.SaveChanges();
                    return StdResult<InstantGagnant>.OkResultInstance(ig);
                }
                else
                    return StdResult<InstantGagnant>.BadResult("Pas d'instant gagnant disponible");
            }
        }
 public static int GetTradeDoublerSequenceNumber()
 {
     using (CollectContext context = new CollectContext())
     {
         var sequenceNumber = (from seq in context.TradeDoublerIndex
                              where seq.Id == 1
                              select seq).FirstOrDefault();
         return sequenceNumber != null ? sequenceNumber.Value : 0;
     }
 }
        public StdResult<InstantGagnant> AddInstantGagnant(InstantGagnant instance)
        {
            using (CollectContext context = new CollectContext())
            {
                context.InstantsGagnants.Add(instance);
                context.SaveChanges();

                return StdResult<InstantGagnant>.OkResultInstance(instance);
            }
        }
        public StdResult<List<AnswerChoice>> GetUsersAnswers(User u)
        {
            using (CollectContext context = new CollectContext())
            {
                if (!context.Users.Any(user => user.Id == u.Id))
                    return StdResult<List<AnswerChoice>>.BadResult("User introuvable en base.");

                var query = context.AnswerChoices.Where(answerChoice => answerChoice.User.Id == u.Id);
                bool alreadyAnswered = query.Any();

                return StdResult<List<AnswerChoice>>.OkResultInstance(query.ToList());
            }
        }
        public List<User> RetrieveNewUsersSince(DateTime since)
        {
            using (CollectContext context = new CollectContext())
            {
                var nouveauxInscrits = from u in context.Users
                                       where u.CreationDate > since
                                       //&& !u.IsCanal
                                       && u.IsOffreGroupCanal
                                       orderby u.CreationDate descending
                                       select u;

                return nouveauxInscrits.ToList();
            }
        }
        public static void SetTradeDoublerSequenceNumber(int value)
        {
            using (CollectContext context = new CollectContext())
            {
                var sequenceNumber = (from seq in context.TradeDoublerIndex
                                      where seq.Id == 1
                                      select seq).FirstOrDefault();
                if (sequenceNumber != null)
                {
                    sequenceNumber.Value = value;

                    context.SaveChanges();
                }
            }
        }
        public StdResult<InstantGagnant> DeleteInstantsGagnant()
        {
            using (CollectContext context = new CollectContext())
            {
                foreach (User user in context.Users)
                {
                    user.InstantsGagnantWon = null;
                }

                foreach (InstantGagnant ig in context.InstantsGagnants)
                {
                    context.InstantsGagnants.Remove(ig);
                }
                context.SaveChanges();

                return StdResult<InstantGagnant>.OkResult;
            }
        }
        public StdResult<InstantGagnant> GetCurrentInstantGagnant()
        {
            using (CollectContext context = new CollectContext())
            {
                DateTime now = DateTime.Now;
                var query = from ig in context.InstantsGagnants
                            where now > ig.StartDateTime
                            && !ig.Won
                            orderby ig.StartDateTime
                            select ig;
                if (query.Any<InstantGagnant>())
                {
                    InstantGagnant ig = query.First<InstantGagnant>();
                    return StdResult<InstantGagnant>.OkResultInstance(ig);
                }
                else
                    return StdResult<InstantGagnant>.BadResult("Pas d'instant gagnant disponible");

            }
        }