ActivEarth.DAO.ChallengeDAO.GetChallengeFromChallengeId C# (CSharp) Method

GetChallengeFromChallengeId() public static method

Retrieves a Challenge from the DB based on its ID.
public static GetChallengeFromChallengeId ( int challengeId, int userId ) : Challenge
challengeId int Identifier of the challenge to retrieve.
userId int Optional. UserID to load challenge progress from.
return ActivEarth.Objects.Competition.Challenges.Challenge
        public static Challenge GetChallengeFromChallengeId(int challengeId, int userId = 0)
        {
            using (SqlConnection connection = ConnectionManager.GetConnection())
            {
                var data = new ActivEarthDataProvidersDataContext(connection);
                Challenge toReturn = (from c in data.ChallengeDataProviders
                        where c.id == challengeId
                        select
                            new Challenge
                            {
                                ID = c.id,
                                Name = c.name,
                                Description = c.description,
                                Reward = c.reward,
                                Requirement = (float)c.requirement,
                                IsPersistent = c.persistent,
                                EndTime = c.end_time,
                                Duration = new TimeSpan(c.duration_days, 0, 0, 0),
                                StatisticBinding = (Statistic)c.statistic,
                                IsActive = c.active,
                                ImagePath = c.image_path
                            }).FirstOrDefault();

                toReturn.Progress = (userId > 0 ? ChallengeManager.GetProgress(toReturn.ID, userId) : 0);
                toReturn.FormatString = StatisticInfoDAO.GetStatisticFormatString(toReturn.StatisticBinding);

                return toReturn;
            }
        }