public static string ExportUserPurchasesByType(VaporStoreDbContext context, string storeType) { //PurchaseType purchaseType = Enum.Parse<PurchaseType>(storeType); var users = context.Users .ToArray() .Where(u => u.Cards.Any(c => c.Purchases.Any(p => p.Type.ToString() == storeType))) .Select(u => new ExportUserDto { Username = u.Username, Purchases = u.Cards .SelectMany(c => c.Purchases) .Where(p => p.Type.ToString() == storeType) .Select(p => new PurchaseDto { Card = p.Card.Number, Cvc = p.Card.Cvc, Date = p.Date.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture), Game = new GameDto { Title = p.Game.Name, Genre = p.Game.Genre.Name, Price = p.Game.Price } }) .OrderBy(p => p.Date) .ToArray(), TotalSpent = u.Cards .Sum(c => c.Purchases.Where(p => p.Type.ToString() == storeType) .Sum(p => p.Game.Price)) }) .OrderByDescending(u => u.TotalSpent) .ThenBy(u => u.Username) .ToArray(); //XmlSerializer xmlSerializer = // new XmlSerializer(typeof(ExportUserDto[]), // new XmlRootAttribute("Users")); //var sw = new StringWriter(); //xmlSerializer.Serialize(sw, users); // return sw.ToString().TrimEnd(); var xml = XmlConverter.Serialize(users, "Users"); return(xml); }
public static string ExportUserPurchasesByType(VaporStoreDbContext context, string storeType) { var userPurchasesByType = context.Users .Include(x => x.Cards) .ThenInclude(x => x.Purchases) .ThenInclude(x => x.Game) .ToList() .Select(x => new UserExportModel { Username = x.Username, Purchases = x.Cards .SelectMany(x => x.Purchases) .Where(x => x.Type.ToString() == storeType) .Select(x => new PurchaseExportModel { CardNumber = x.Card.Number, CvC = x.Card.Cvc, Date = x.Date.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture), Game = new GameExportModel { Title = x.Game.Name, GenreName = x.Game.Genre.Name, Price = x.Game.Price }, }) .OrderBy(x => x.Date) .ToArray(), TotalSpent = x.Cards .Sum(x => x.Purchases.Where(x => x.Type.ToString() == storeType) .Sum(x => x.Game.Price)) }) .Where(x => x.Purchases.Any()) .OrderByDescending(x => x.TotalSpent) .ThenBy(x => x.Username) .ToArray(); ; var xml = XmlConverter.Serialize <UserExportModel[]>(userPurchasesByType, "Users"); return(xml); }
public static string ExportUserPurchasesByType(VaporStoreDbContext context, string storeType) { var purchaseType = Enum.Parse <PurchaseType>(storeType); var sortedUsers = context .Users .ToList() .Where(u => u.Cards.Any(c => c.Purchases .Any(p => p.Type.ToString() == storeType))) .Select(u => new UserPurchasesByTypeExportModel() { Username = u.Username, TotalSpent = u.Cards .Sum(c => c.Purchases.Where(p => p.Type.ToString() == storeType) .Sum(p => p.Game.Price)), Purchases = u.Cards.SelectMany(p => p.Purchases) .Where(p => p.Type.ToString() == storeType) .Select(p => new PurchaseExportModel() { Card = p.Card.Number, Cvc = p.Card.Cvc, Date = p.Date.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture), Game = new GameExportModel() { Title = p.Game.Name, Genre = p.Game.Genre.Name, Price = p.Game.Price } }) .OrderBy(p => p.Date) .ToArray() }) .OrderByDescending(x => x.TotalSpent) .ThenBy(x => x.Username) .ToList(); var xmlResult = XmlConverter.Serialize(sortedUsers, "Users"); return(xmlResult); }
public static string ExportUserPurchasesByType(VaporStoreDbContext context, string storeType) { var users = context .Users .ToList() .Where(x => x.Cards.Any(c => c.Purchases.Any(p => p.Type.ToString() == storeType))) .Select(x => new UserPurchaseOutputModel { Username = x.Username, TotalSpent = x.Cards.Sum( c => c.Purchases .Where(p => p.Type.ToString() == storeType) .Sum(x => x.Game.Price)), Purchases = x.Cards.SelectMany(c => c.Purchases) .Where(p => p.Type.ToString() == storeType) .Select(p => new PurchaseOutputModel { Card = p.Card.Number, Cvc = p.Card.Cvc, Date = p.Date.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture), Game = new GameOutputModel { Title = p.Game.Name, Price = p.Game.Price, Genre = p.Game.Genre.Name } }) .OrderBy(x => x.Date) .ToArray() }) .OrderByDescending(x => x.TotalSpent) .ThenBy(x => x.Username) .ToArray(); string root = "Users"; string xmlResult = XmlConverter.Serialize(users, root); return(xmlResult); }
public static string ExportUserPurchasesByType(VaporStoreDbContext context, string storeType) { var users = context.Users .ToArray() .Where(x => x.Cards.Any(p => p.Purchases.Any())) .Select(x => new ExportUserDto { username = x.Username, Purchases = context.Purchases .ToArray() .Where(c => c.Card.User.Username == x.Username && c.Type.ToString() == storeType) .Select(p => new ExportPurchaseDto() { Card = p.Card.Number, Cvc = p.Card.Cvc, Date = p.Date.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture), Game = new ExportGameDto() { title = p.Game.Name, Genre = p.Game.Genre.Name, Price = p.Game.Price } }) .OrderBy(o => DateTime.ParseExact(o.Date, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture)) .ToArray(), TotalSpent = context.Purchases .ToArray() .Where(c => c.Card.User.Username == x.Username && c.Type.ToString() == storeType).Sum(s => s.Game.Price) }) .Where(x => x.Purchases.Any()) .OrderByDescending(o => o.TotalSpent) .ThenBy(o => o.username) .ToArray(); string xml = XmlConverter.Serialize(users, "Users"); return(xml); }
public static string ExportUserPurchasesByType(VaporStoreDbContext context, string storeType) { var userPurchases = context.Users .ToArray() .Where(x => x.Cards.Any(y => y.Purchases.Any())) .Select(x => new UserPurchasesExportModel { Username = x.Username, Purchases = context.Purchases .ToArray() .Where(p => p.Card.User.Username == x.Username && p.Type.ToString() == storeType) .OrderBy(p => p.Date) .Select(p => new PurchaseExportModel { CardNumber = p.Card.Number, Cvc = p.Card.Cvc, Date = p.Date.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture), Game = new GameExportModel() { GameName = p.Game.Name, Genre = p.Game.Genre.Name, Price = p.Game.Price } }) .ToArray(), TotalSpent = context.Purchases.ToArray().Where(p => p.Card.User.Username == x.Username && p.Type.ToString() == storeType).Sum(p => p.Game.Price) }) .Where(u => u.Purchases.Length > 0) .OrderByDescending(x => x.TotalSpent) .ThenBy(x => x.Username) .ToList(); var xml = XmlConverter.Serialize(userPurchases, "Users"); return(xml); }
public static string ExportUserPurchasesByType(VaporStoreDbContext context, string storeType) { var data = context.Users .ToArray() .Where(u => u.Cards.Any(c => c.Purchases.Any(p => p.Type.ToString() == storeType))) .Select(u => new UserPurchasesXmlModel { Username = u.Username, Purchases = u.Cards.SelectMany(c => c.Purchases) .Where(c => c.Type.ToString() == storeType) .Select(p => new PurchaseXmlModel { Card = p.Card.Number, Cvc = p.Card.Cvc, Date = p.Date.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture), Game = new GameXmlModel { Title = p.Game.Name, Genre = p.Game.Genre.Name, Price = p.Game.Price } }) .OrderBy(p => p.Date) .ToArray(), TotalSpent = u.Cards.SelectMany(c => c.Purchases) .Where(c => c.Type.ToString() == storeType) .Sum(p => p.Game.Price) }) .OrderByDescending(u => u.TotalSpent) .ThenBy(u => u.Username) .ToArray(); var xml = XmlConverter.Serialize(data, "Users"); return(xml); }
public static string ExportUserPurchasesByType(VaporStoreDbContext context, string storeType) { var users = context.Users.ToList().Where(x => x.Cards.Any(a => a.Purchases.Any(p=>p.Type.ToString()==storeType))) .Select(x => new UsersXmlExportModel() { Username = x.Username, Purchases = x.Cards.SelectMany(c => c.Purchases) .Where(pp=>pp.Type.ToString()==storeType) .Select(s => new PurchasesXmlOutputModel { Card = s.Card.Number, Cvc = s.Card.Cvc, Date = s.Date.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture), Game = new GameXmlOutputModel() { Title = s.Game.Name, Genre = s.Game.Genre.Name, Price = s.Game.Price, } }) .OrderBy(o=>o.Date) .ToArray(), TotalSpent = x.Cards.Sum(s => s.Purchases.Where(p => p.Type.ToString() == storeType).Sum(su => su.Game.Price)) }) .OrderByDescending(x=>x.TotalSpent) .ThenBy(x=>x.Username) .ToList(); //var users = context.Users.Where(x => x.Cards.Any(c => c.Purchases.Any())) // .SelectMany(x=>x.Cards, (x,c) => new // { // x.Username, // Purchases = c.Purchases.Where(p=>p.Type.ToString() == storeType).Select(s=> new // { // Card = s.Card.Number, // Cvc = s.Card.Cvc, // Date = s.Date.ToString("yyyy-MM-dd HH:mm",CultureInfo.InvariantCulture), // Game = s.Game.Name, // GameGenre = s.Game.Genre.Name, // GamePrice = s.Game.Price // }), // }).ToList(); //var usersList = new List<UsersXmlExportModel>(); //foreach (var user in users) //{ // var newUser = new UsersXmlExportModel // { // Username = user.Username, // }; // var list = new List<PurchasesXmlOutputModel>(); // foreach (var purchase in user.Purchases) // { // var game = new GameXmlOutputModel() // { // Genre = purchase.GameGenre, // Price = purchase.GamePrice, // Title = purchase.Game // }; // var purchaseForUser = new PurchasesXmlOutputModel // { // Card = purchase.Card, // Game = game, // Cvc = purchase.Cvc, // Date = purchase.Date // }; // list.Add(purchaseForUser); // } // newUser.Purchases = list.ToArray(); // newUser.TotalSpent = 69; // usersList.Add(newUser); //} var result = XmlConverter.Serialize(users, "Users"); return result; }