static void Main()
        {
            var context = new DiabloEntities();
            var games = context.Games
                .Where(g => g.IsFinished == true)
                .OrderBy(g => g.Name)
                .Select(g => new
                {
                    g.Name,
                    g.Duration,
                    Users = g.UsersGames
                        .Select(ug => new
                        {
                            ug.User.Username,
                            ug.User.IpAddress
                        })
                });

            XmlWriterSettings settings = new XmlWriterSettings()
            {
                Indent = true,
                NewLineChars = "\n"
            };

            using (XmlWriter writer = XmlWriter.Create("../../finished-games.xml", settings))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("games");

                foreach (var game in games)
                {
                    writer.WriteStartElement("game");
                    writer.WriteAttributeString("name", game.Name);

                    if (game.Duration != null)
                    {
                        writer.WriteAttributeString("duration", game.Duration.ToString());
                    }

                    writer.WriteStartElement("users");

                    foreach (var user in game.Users)
                    {
                        writer.WriteStartElement("user");
                        writer.WriteAttributeString("username", user.Username);
                        writer.WriteAttributeString("ip-address", user.IpAddress);
                        writer.WriteEndElement();
                    }

                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
        }
        static void Main()
        {
            var context = new DiabloEntities();
            var characters = context.Characters
                .Select(c => c.Name);

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

            var finishedGames = context.Games
                .Where(g => g.IsFinished == true)
                .OrderBy(g => g.Name)
                .ThenBy(g => g.Duration)
                .Select(g => new
                {
                    GameName = g.Name,
                    GameDuration = g.Duration,
                    Users = new
                    {
                        user = g.UsersGames.Select(ug => ug.User)
                    }
                });

            var resultXml = new XElement("games");

            foreach (var game in finishedGames)
            {
                var gameXml = new XElement("game");
                gameXml.Add(new XAttribute("name", game.GameName));
                if (game.GameDuration.HasValue)
                {
                    gameXml.Add(new XAttribute("duration", game.GameDuration));
                }

                var users = new XElement("users");

                foreach (var user in game.Users.user)
                {
                    users.Add(new XElement("user",
                        new XAttribute("username", user.Username),
                        new XAttribute("ip-address", user.IpAddress)));
                }

                gameXml.Add(users);

                resultXml.Add(gameXml);

            }
            var resultXmlDoc = new XDocument();
            resultXmlDoc.Add(resultXml);
            resultXmlDoc.Save("../../finished-games.xml");

            Console.WriteLine("Finished Games exported to finished-games.xml");
        }
        static void Main()
        {
            var context = new DiabloEntities();

            var charactersQ = context.Characters
                              .Select(c => new
            {
                c.Name
            });

            foreach (var character in charactersQ)
            {
                Console.WriteLine(character.Name);
            }
        }
        static void Main()
        {
            var context = new DiabloEntities();
            var characters = context.Characters
                .OrderBy(c => c.Name)
                .Select(c => new
                {
                    name = c.Name,
                    playedBy = c.UsersGames
                        .Select(ug => ug.User.Username)
                });

            string json = JsonConvert.SerializeObject(characters, Formatting.Indented);
            File.WriteAllText("../../characters.json", json);
        }
        static void Main()
        {
            var context = new DiabloEntities();

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

            var JSerializer = new JavaScriptSerializer();
            var charactersAndUsers = JSerializer.Serialize(charactersAndPlayers);
            File.WriteAllText("../../characters.json", charactersAndUsers);
            Console.WriteLine("File characters.json exported.");
        }
        static void Main()
        {
            var context = new DiabloEntities();
            XDocument xmlDoc = XDocument.Load("../../users-and-games.xml");

            var xUsers = xmlDoc.XPathSelectElements("/users/user");

            foreach (var xUser in xUsers)
            {
                User user = CreateUserIfNotExists(context, xUser);

            }
        }
        private static User CreateUserIfNotExists(DiabloEntities context, XElement xUser)
        {
            User user = null;
            var xUsername = xUser.Attribute("username").Value;

            user = context.Users.FirstOrDefault(u => u.Username == xUsername);

            if (user != null)
            {
                Console.WriteLine("User {0} already exists", xUsername);
            }

            else
            {
                var isDeleted = true;
                if (int.Parse(xUser.Attribute("is-deleted").Value) == 0)
                {
                    isDeleted = false;
                }

                var ip = xUser.Attribute("ip-address").Value;
                var regDate = xUser.Attribute("registration-date").Value;
                string format = "dd/MM/yyyy";
                DateTime regDateToImport = DateTime.ParseExact(regDate, format, CultureInfo.InvariantCulture);

                var firstNameAtt = xUser.Attribute("first-name");
                var lastNameAtt = xUser.Attribute("last-name");
                var emailAtt = xUser.Attribute("email");

                string firstName = null;
                string lastName = null;
                string email = null;

                if (firstNameAtt != null)
                {
                    firstName = firstNameAtt.Value;
                }

                if (lastNameAtt != null)
                {
                    lastName = lastNameAtt.Value;
                }

                if (emailAtt != null)
                {
                    email = emailAtt.Value;
                }

                user = new User
                {
                    Username = xUsername,
                    FirstName = firstName,
                    LastName = lastName,
                    Email = email,
                    IpAddress = ip,
                    RegistrationDate = regDateToImport,
                    IsDeleted = isDeleted
                };

                context.Users.Add(user);
                context.SaveChanges();
                Console.WriteLine("Successfully added user {0}", xUsername);
            }

            return user;
        }
        static void Main()
        {
            var context = new DiabloEntities();
            var xmlDocument = XDocument.Load("../../users-and-games.xml");
            var users =
                from user in xmlDocument.Descendants("user")
                select new
                {
                    FirstName = user.Attribute("first-name") == null ? null : user.Attribute("first-name").Value,
                    LastName = user.Attribute("last-name") == null ? null : user.Attribute("last-name").Value,
                    Username = user.Attribute("username").Value,
                    Email = user.Attribute("email") == null ? null : user.Attribute("email").Value,
                    IsDeleted = byte.Parse(user.Attribute("is-deleted").Value) == 0 ? false : true,
                    IpAddress = user.Attribute("ip-address").Value,
                    RegistrationDate = new DateTime(int.Parse(user.Attribute("registration-date").Value.Split('/')[2]),
                        int.Parse(user.Attribute("registration-date").Value.Split('/')[1]),
                        int.Parse(user.Attribute("registration-date").Value.Split('/')[0])),
                    Games =
                        from game in user.Descendants("game")
                        select new
                        {
                            GameName = game.Element("game-name").Value,
                            CharacterName = game.Element("character").Attribute("name").Value,
                            Cash = decimal.Parse(game.Element("character").Attribute("cash").Value),
                            Level = int.Parse(game.Element("character").Attribute("level").Value),
                            JoinedOn = new DateTime(int.Parse(game.Element("joined-on").Value.Split('/')[2]),
                                int.Parse(game.Element("joined-on").Value.Split('/')[1]),
                                int.Parse(game.Element("joined-on").Value.Split('/')[0]))
                        }
                };

            foreach (var user in users)
            {
                if (!context.Users.Any(u => u.Username == user.Username))
                {
                    context.Users.Add(new User()
                    {
                        FirstName = user.FirstName,
                        LastName = user.LastName,
                        Username = user.Username,
                        Email = user.Email,
                        IsDeleted = user.IsDeleted,
                        IpAddress = user.IpAddress,
                        RegistrationDate = user.RegistrationDate
                    });
                    context.SaveChanges();
                    Console.WriteLine("Successfully added user {0}", user.Username);

                    foreach (var game in user.Games)
                    {
                        context.UsersGames.Add(new UsersGame()
                        {
                            GameId = context.Games.Where(g => g.Name == game.GameName).FirstOrDefault().Id,
                            UserId = context.Users.Where(u => u.Username == user.Username).FirstOrDefault().Id,
                            CharacterId = context.Characters.Where(c => c.Name == game.CharacterName).FirstOrDefault().Id,
                            Level = game.Level,
                            JoinedOn = game.JoinedOn,
                            Cash = game.Cash
                        });
                        Console.WriteLine("User {0} successfully added to game {1}", user.Username, game.GameName);
                    }
                }
                else
                {
                    Console.WriteLine("User {0} already exists", user.Username);
                }
            }

            context.SaveChanges();
        }