public static void Main() { CheckExistingDirectory(ExportPath); var context = new DiabloEntities(); var characters = context.Characters .OrderBy(c => c.Name) .Select(c => new { c.Name, Players = context.UsersGames.Where(g => g.CharacterId == c.Id).Select(g => g.User.Username) }); var json = JsonConvert.SerializeObject(characters, Formatting.Indented); File.WriteAllText(ExportPath + Filename, json); Console.WriteLine("File path: {0}", Path.GetFullPath(ExportPath + Filename)); }
private static UsersGame ParseGameData(DiabloEntities context, XElement game) { var gameName = game.Element("game-name").Value; var character = game.Element("character"); var characterName = character.Attribute("name").Value; var cash = decimal.Parse(character.Attribute("cash").Value); var level = int.Parse(character.Attribute("level").Value); var joinedOn = DateTime.ParseExact(game.Element("joined-on").Value, "dd/MM/yyyy", CultureInfo.InvariantCulture); var newGame = new UsersGame { Game = context.Games.First(g => g.Name == gameName), Character = context.Characters.First(c => c.Name == characterName), Cash = cash, Level = level, JoinedOn = joinedOn }; return newGame; }
private static void ParseXmlDocument(DiabloEntities context, string filePath) { var doc = XDocument.Load(filePath); var users = doc.Descendants("user"); foreach (var user in users) { try { var username = user.Attribute("username").Value; if (context.Users.Any(u => u.Username == username)) { throw new InvalidOperationException(string.Format("User {0} already exists", username)); } var output = new StringBuilder(); User newUser = ParseUserData(user, username, output); output.AppendFormat("Successfully added user {0}", username).AppendLine(); foreach (var game in user.Descendants("game")) { var newUserGame = ParseGameData(context, game); newUser.UsersGames.Add(newUserGame); output.AppendFormat("User {0} successfully added to game {1}", username, newUserGame.Game.Name) .AppendLine(); } context.Users.Add(newUser); context.SaveChanges(); Console.WriteLine(output); } catch (Exception e) { Console.WriteLine(e.Message); } } }
public static void Main() { CheckExistingDirectory(ExportPath); var context = new DiabloEntities(); var games = context.Games .Where(g => g.IsFinished) .OrderBy(g => g.Name) .ThenBy(g => g.Duration) .Select(g => new FinishedGame { Name = g.Name, Duration = g.Duration, Users = context.UsersGames .Where(ug => ug.GameId == g.Id).Select(ug => new User { Username = ug.User.Username, IpAddress = ug.User.IpAddress }) }); ExportGamesToXml(games); }
public static void Main() { var context = new DiabloEntities(); context.Characters.Select(c => c.Name).ToList().ForEach(Console.WriteLine); }
public static void Main() { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; var context = new DiabloEntities(); ParseXmlDocument(context, ImportFilePath); }