public static IQueryable<CityInfo> GetCities(string username)
        {
            IQueryable<CityInfo> cities;
            using (var db = new DatabaseContext())
            {
                IQueryable<CityInfo> query = from c in db.CityInfoes
                        where c.User.Equals(username)
                        select c;

                cities = query.ToList().AsQueryable();
            }

            return cities;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static IQueryable<RuleSet> GetRuleSets(string username)
        {
            IQueryable<RuleSet> ruleSets;

            using (var db = new DatabaseContext())
            {
                IQueryable<RuleSet> query =
                    from c in db.RuleSets
                    where c.User.Equals(username)
                    select c;
                ruleSets = query.ToList().AsQueryable();
            }

            return ruleSets;
        }
        public static double GetValue(string scoringIdName, CityInfo city, DatabaseContext db)
        {
            ScoringIdentifier scoringId = db.ScoringIdentifiers.First(s => s.Name.Equals(scoringIdName) || s.ShortName.Equals(scoringIdName));

            object objVal = city.GetType().GetProperty(scoringId.PropertyName).GetValue(city);
            double value;

            if (objVal.GetType() == typeof(int))
            {
                value = (double)(int)objVal;
            }
            else
            {
                // Identifier validity should have already been checked, so this should be a double.
                value = (double)objVal;
            }

            return value;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public static void DeleteRuleSet(int id)
        {
            using (var db = new DatabaseContext())
            {
                RuleSet ruleSet = db.RuleSets.First(i => i.RuleSetId == id);
                db.RuleSets.Remove(ruleSet);

                // Remove references to this rule set (formula) from any rankings in which it appears.
                var rankings = from r in db.Rankings
                               where r.RuleSetId == ruleSet.RuleSetId
                               select r;
                foreach (Ranking ranking in rankings)
                {
                    ranking.RuleSetId = -1;
                }

                db.SaveChanges();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ruleset"></param>
        public static void CreateRuleSet(string name, string formula, string username)
        {
            DateTime now = DateTime.Now;
            bool isValidFormula = ValidateFormula(formula);

            var ruleSet = new RuleSet
            {
                RuleSetName = name,
                Formula = formula,
                User = username,
                Created = now,
                Valid = isValidFormula
            };

            using (var db = new DatabaseContext())
            {
                db.RuleSets.Add(ruleSet);
                db.SaveChanges();
            }
        }
        public static void DeleteCity(int cityId)
        {
            using (var db = new DatabaseContext())
            {
                CityInfo cityInfo = db.CityInfoes.First(i => i.CityInfoId == cityId);
                db.CityInfoes.Remove(cityInfo);

                // Remove the city from any rankings it was present in.
                var rankedRefs = from r in db.RankingMembers
                                 where r.CityInfoId == cityInfo.CityInfoId
                                 select r;
                foreach (RankingMember rankedReference in rankedRefs)
                {
                    db.RankingMembers.Remove(rankedReference);
                }

                db.SaveChanges();
            }

            // TODO: Also delete city files... or mark them as "orphan" somehow.
            //       Maybe the city ref should just be marked as orphan in the db... that way we can save the ranked instances.
        }
 public static bool IsValueIdentifier(string canditateString, DatabaseContext db)
 {
     return db.ScoringIdentifiers.Any(s => s.Name == canditateString || s.ShortName == canditateString);
 }
        public static IQueryable<ScoringIdentifier> GetScoringIdentifiers()
        {
            IQueryable<ScoringIdentifier> identifiers;
            var db = new DatabaseContext();

            identifiers = from s in db.ScoringIdentifiers
                          orderby s.DisplayOrder
                          select s;

            return identifiers;
        }
        private static string getBadFormulaIds(string formula, List<string> cityIds)
        {
            // Make sure all value identifiers are valid.

            string badIds = "";
            using (var db = new DatabaseContext())
            {
                foreach (string id in cityIds)
                {
                    if (!GetCityValue.IsValueIdentifier(id, db))
                    {
                        badIds = badIds + id + " ";
                    }
                }
            }
            return badIds;
        }
        public static void UpdateScoringIdentifiers(string serverHome)
        {
            using (DatabaseContext db = new DatabaseContext())
            {
                // Clear all scoring ids.
                // ... inefficiently, but this only runs at application start so it will do.
                foreach (var scoringId in db.ScoringIdentifiers)
                {
                    db.ScoringIdentifiers.Remove(scoringId);
                }

                // Add all scoring ids.
                foreach (var scoringId in getScoringIdentifiers(serverHome))
                {
                    db.ScoringIdentifiers.Add(scoringId);
                }

                db.SaveChanges();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public static bool IsDuplicateName(string name, string username)
        {
            bool result;
            using (var db = new DatabaseContext())
            {
                result = db.RuleSets.Any(r => r.User.Equals(username) && r.RuleSetName.Equals(name));
            }

            return result;
        }
        private static void storeCity(City parserCity, string username, string filepath, Stream cityFileStream)
        {
            // Fetch relevant data from parserCity.
            var city = new CityInfo(parserCity, username, filepath, DateTime.Now);

            // Save .sc2 file on the server.
            cityFileStream.Position = 0;
            using (Stream outputStream = File.OpenWrite(filepath))
            {
                cityFileStream.CopyTo(outputStream);
            }

            // Save parsed city data to database.
            using (var db = new DatabaseContext())
            {
                db.CityInfoes.Add(city);
                db.SaveChanges();
            }
        }