static void Main()
        {
            var context = new DiabloEntities();

            var characterNames = context.Characters.Select(c => c.Name);

            foreach (var name in characterNames)
            {
                Console.WriteLine(name);
            }
        }
        static void Main()
        {
            var context = new DiabloEntities();

            var characterNames = context.Characters.Select(c => c.Name);

            foreach (var name in characterNames)
            {
                Console.WriteLine(name);
            }
        }
        static void Main()
        {
            var context = new DiabloEntities();

            var finishedGames = context.Games
                .Where(g => g.IsFinished)
                .OrderBy(g => g.Name)
                .ThenBy(g => g.Duration)
                .Select(g => new
                {
                    Name = g.Name,
                    Duration = g.Duration,
                    Users = g.UsersGames.Select(ug => new
                    {
                        Username = ug.User.Username,
                        IPAddress = ug.User.IpAddress
                    })
                });

            var doc = new XDocument();
            var rootElem = new XElement("games");

            foreach (var finishedGame in finishedGames)
            {
                if (finishedGame.Name != null)
                {
                    var gameElem = new XElement("game", new XAttribute("name", finishedGame.Name));

                    if (finishedGame.Duration != null)
                    {
                        gameElem.Add(new XAttribute("duration", finishedGame.Duration));
                    }

                    var usersElem = new XElement("users");

                    foreach (var user in finishedGame.Users)
                    {
                        var userElem = new XElement("user", new XAttribute("username", user.Username), new XAttribute("ip-address", user.IPAddress));
                        usersElem.Add(userElem);
                    }

                    gameElem.Add(usersElem);
                    rootElem.Add(gameElem);
                }
            }

            doc.Add(rootElem);
            doc.Save("../../finished-games.xml");
        }
        static void Main()
        {
            var context = new DiabloEntities();

            var charactersPlayer = context.Characters
                .OrderBy(c => c.Name)
                .Select(c => new
                {
                    name = c.Name,
                    playedBy = c.UsersGames.Select(ug => ug.User.Username)
                });

            var json = new JavaScriptSerializer().Serialize(charactersPlayer);
            File.WriteAllText("../../characters.json", json);
        }
        static void Main()
        {
            var context = new DiabloEntities();

            var xml = new XmlDocument();
            xml.Load("../../users-and-games.xml");
            var rootNode = xml.DocumentElement;

            foreach (XmlNode user in rootNode.ChildNodes)
            {
                var username = user.Attributes["username"].Value;
                var ipAddress = user.Attributes["ip-address"].Value;
                DateTime registrationDate = DateTime.Parse(user.Attributes["registration-date"].Value);
                bool isDeleted = false;
                string lastName = null;
                string firstName = null;
                string email = null;

                if (user.Attributes["first-name"] != null)
                {
                    firstName = user.Attributes["first-name"].Value;
                }
                if (user.Attributes["last-name"] != null)
                {
                    lastName = user.Attributes["last-name"].Value;
                }
                if (user.Attributes["email"] != null)
                {
                    email = user.Attributes["email"].Value;
                }
                if (int.Parse(user.Attributes["is-deleted"].Value) == 1)
                {
                    isDeleted = true;
                }

                if (context.Users.Any(u => u.Username == username))
                {
                    Console.WriteLine("User {0} already exists", username);
                    continue;
                }

                var userGames = rootNode.SelectSingleNode("user/games");

                // Loop through all games for the given user
                foreach (XmlNode game in userGames)
                {
                    var gameName = game["game-name"].InnerText;
                    var character = game["character"];

                    var characterName = character.Attributes["name"].Value;
                    var characterCash = decimal.Parse(character.Attributes["cash"].Value);
                    var characterLevel = int.Parse(character.Attributes["level"].Value);
                    var joinedOn = DateTime.Parse(game["joined-on"].InnerText);
                    Console.WriteLine(joinedOn);

                    if (game["joined-on"] != null && user.Attributes["registration-date"] != null)
                    {
                        var userGame = new UsersGame()
                        {
                            Cash = characterCash,
                            Character = context.Characters.FirstOrDefault(c => c.Name == characterName),
                            Game = context.Games.FirstOrDefault(g => g.Name == gameName),
                            JoinedOn = joinedOn,
                            Level = characterLevel,
                            User = new User()
                            {
                                FirstName = firstName,
                                LastName = lastName,
                                Email = email,
                                Username = username,
                                IpAddress = ipAddress,
                                RegistrationDate = registrationDate,
                                IsDeleted = isDeleted
                            }
                        };

                        context.UsersGames.Add(userGame);

                        Console.WriteLine("Successfully added user {0}", username);
                        Console.WriteLine("User {0} successfully added to game {1}", username, gameName);
                    }
                }

                context.SaveChanges();
            }
        }