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) }); var json = new JavaScriptSerializer().Serialize(characters); File.WriteAllText(@"../../characters.json", json); }
static void Main() { var context = new DiabloEntities(); var playerNames = context.Users.Select(u => new { Name = u.FirstName + " " + u.LastName }); foreach (var player in playerNames) { Console.WriteLine("Name: {0}", player.Name); } }
static void Main() { var context = new DiabloEntities(); var finishedGames = context.Games.Where(g => g.IsFinished).OrderBy(g => g.Name).Select(g => new { Name = g.Name, Duration = g.Duration, Usernames = g.UsersGames.Select(ug => ug.User.Username), IpAddresses = g.UsersGames.Select(ug => ug.User.IpAddress) }); XElement games = new XElement("games"); foreach (var game in finishedGames) { XElement users = new XElement("users"); XElement xmlGame = new XElement("game", new XAttribute("name", game.Name) ); if (game.Duration != null) { xmlGame.Add(new XAttribute("duration", game.Duration)); } for (int i = 0; i < game.Usernames.Count(); i++) { XElement xmlUser = new XElement("user", new XAttribute("username", game.Usernames.ElementAt(i)), new XAttribute("ip-address", game.IpAddresses.ElementAt(i)) ); xmlGame.Add(xmlUser); } games.Add(xmlGame); } games.Save("../../finished-games.xml"); }
static void Main() { var context = new DiabloEntities(); XmlDocument doc = new XmlDocument(); doc.Load("../../users-and-games.xml"); var root = doc.DocumentElement; foreach (XmlNode xmlUser in root.ChildNodes) { XmlNode userNode = xmlUser.SelectSingleNode("user"); User user = null; string gameName = null; Character character = null; foreach (XmlAttribute xmlUserAttributes in userNode) { string firstName = xmlUserAttributes.Attributes["first-name"].Value; string lastName = xmlUserAttributes.Attributes["last-name"].Value; string username = xmlUserAttributes.Attributes["username"].Value; string email = xmlUserAttributes.Attributes["email"].Value; Boolean isDeleted = Boolean.Parse(xmlUserAttributes.Attributes["is-deleted"].Value); string ipAddress = xmlUserAttributes.Attributes["ip-address"].Value; DateTime registrationDate = DateTime.Parse(xmlUserAttributes.Attributes["registration-date"].Value); if (username != null && isDeleted != true && ipAddress != null && registrationDate != null) { User currentUser = null; currentUser = context.Users.FirstOrDefault(u => u.Username == username && u.RegistrationDate == registrationDate); if (currentUser != null) { Console.WriteLine("User {0} already exists", username); } else { user = new User() { FirstName = firstName, LastName = lastName, Username = username, Email = email, IsDeleted = isDeleted, IpAddress = ipAddress, RegistrationDate = registrationDate }; } } } foreach (XmlNode xmlGame in userNode.ChildNodes) { XmlNode gameNode = xmlGame.SelectSingleNode("game"); gameName = gameNode.SelectSingleNode("game-name").Value; XmlNode characterNode = gameNode.SelectSingleNode("character"); string characterName = null; decimal cash = 0; int level = 0; foreach (XmlAttribute xmlCharacter in characterNode) { characterName = xmlCharacter.Attributes["name"].Value; cash = decimal.Parse(xmlCharacter.Attributes["name"].Value); level = int.Parse(xmlCharacter.Attributes["name"].Value); } Character currentCharacter = null; currentCharacter = context.Characters.FirstOrDefault(c => c.Name == characterName); if (currentCharacter == null) { character = new Character() { Name = characterName, Statistic = new Statistic() { } }; } } } }