1

Orders are created and saved in this method

public async Task<ActionResult> FirstClassCreate(FormCollection values)
    {
        var order = new Order();
        TryUpdateModel(order);
        var customer = db.Users.FirstOrDefault(x => x.Email == User.Identity.Name);
        var cart = ShoppingCart.GetCart(this.HttpContext);

        try
        {
            order.DeliveryDate = DateTime.Now.AddDays(1);

            order.DeliveryMethod = "First Class";
            order.FirstName = customer.FirstName;
            order.LastName = customer.LastName;
            order.PostalCode = customer.PostalCode;
            order.State = customer.State;
            order.City = customer.City;
            order.Email = customer.Email;
            order.Country = customer.Country;
            order.Phone = customer.PhoneNumber;
            order.Address = customer.Address;
            order.HasPaid = false;
            order.Username = customer.Email;
            order.OrderDate = DateTime.Now;
            var currentUserId = User.Identity.GetUserId();
            order.Total = cart.GetFirstClass();

            if (order.SaveInfo && !order.Username.Equals("guest@guest.com"))
            {
                var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
                var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
                var ctx = store.Context;
                var currentUser = manager.FindById(User.Identity.GetUserId());

                //Save this back
                //http://stackoverflow.com/questions/20444022/updating-user-data-asp-net-identity
                //var result = await UserManager.UpdateAsync(currentUser);
                await ctx.SaveChangesAsync();

                await storeDB.SaveChangesAsync();
            }

            //Save Order
            storeDB.Orders.Add(order);
            await storeDB.SaveChangesAsync();

            //Process the order
            cart = ShoppingCart.GetCart(this.HttpContext);
            order.Total = cart.GetFirstClass();

            order = cart.CreateOrder(order);

            return RedirectToAction("FirstClass", "Checkouts", order);
        }
        catch
        {
            //Invalid - redisplay with errors
            return View(order);
        }
    }   

I need to be access the orders database and change attributes of a specific order, using the email as the Unique Identifier and search for the newest (find the newest using the Order Date where 'haspaid' = false).

 using ()//Insert database context here)
        {
            //Search for order in the database, try using the email as the Unique Identifire and search for the newest where haspaid = false

            //Change the haspaid attribute to true
            var orders = from o in db.Orders
                         where o.Email == User.Identity.Name, o.HasPaid = false, //Newest
                         select o;
            order.HasPaid = true;
            db.Orders.SaveChanges();
             //save database changes
        }
kayess
  • 3,384
  • 9
  • 28
  • 45
  • 1
    What problem are you facing or you want someone to write code on your behalf? – Siva Gopal May 25 '17 at 14:24
  • Your question is quite unclear. What exactly is your problem on doing so? Do you get an exception? Or are the results not stored to the DB? Or what else is your problem? – MakePeaceGreatAgain May 25 '17 at 14:27
  • **Unique** means there is only 1, why would you need to filter further? Is there no _**OrderID**_ in your stucture? – Mad Myche May 25 '17 at 14:38
  • There was an order id but it is linked to the users email so it would be a problem if a user had more than one order – user8064545 May 25 '17 at 15:50

1 Answers1

1

If I understand you correctly, you don't know how to query the record you want to update. So you need to use the Where() to filter the record and use the Max() function to get the latest date.

var orderToBeChanged = db.Orders
    .Where(o => o.HasPaid == false && o => o.Email == User.Identity.Name)
    .Max(o => o.OrderDate);
kayess
  • 3,384
  • 9
  • 28
  • 45