-1

This is my code I want to delete a complete data from cart table of a given user id. this is my cart table

[HttpDelete]
    public HttpResponseMessage DeleteCart(int id)
    {
        if (IsNetworkAvailable(0))
        {

            var del = db.Carts.Where(e => e.UID == id);
           var userid = db.Users.All(e => e.ID ==del);
            db.Foods.Remove(del);
            db.SaveChanges();

            return Request.CreateResponse(HttpStatusCode.OK, "Delete Successfully");
        }
        else
        {
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "No Network");
        }

the problem is in LInq statements.

3
  • 1
    No, none of that really makes any sense... Why get the user, what does food have to do with cart or user. I imagine you are getting a lot of compile time errors, please include them. Have you thought about following an EF tutorial? Maybe that would answer a lot of your questions on how to do this..
    – Igor
    Commented Feb 25, 2019 at 16:53
  • Also give meaningful names to your variables, parameters, and properties. id, UID, ID, del.... these names make your code difficult to read and convey no meaning.
    – Igor
    Commented Feb 25, 2019 at 16:56
  • @Igor i want to achieve that when ever i give a user id it will delete complete cart data of that user Commented Feb 25, 2019 at 16:56

2 Answers 2

0

i want to achieve that when ever i give a user id it will delete complete cart data

Assuming your model is that a Cart instance contains zero to many Food instances you would do the following:

var foodsToDelete = db.Foods.Where(_ => _.Cart.UID == id).ToList();
db.Foods.RemoveRange(foodsToDelete);
db.SaveChanges();

That would "clear the cart data" but not delete the cart itself.

0

I think this is what you should do

    [HttpDelete]
    public HttpResponseMessage DeleteCart(int id)
    {
        //not sure what it does?
        if (IsNetworkAvailable(0))
        {

            var del = db.Carts.First(e => e.Id == id);
            db.Carts.Remove(del);
            db.SaveChanges();

            return Request.CreateResponse(HttpStatusCode.OK, "Delete Successfully");
        }
        else
        {
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "No Network");
        }
    }

you can also think about using try/catch blocks when saving the changes after deletion to handle unexpected exceptions and return proper message back to the client.

You should avoid making redundant calls to the database in your backend. I believe that if you only want to remove an item from a given table, you should only send id of this item. If you have a problem to deliver the id of the item then there may be a problem with the implementation of the client side.

Edit

i want to achieve that when ever i give a user id it will delete complete cart data of that user

        [HttpDelete]
        public HttpResponseMessage DeleteUserCarts(int userId)
        {
            //not sure what it does?
            if (IsNetworkAvailable(0))
            {

                var carts = db.Carts.Where(e => e.UID == userId);
                db.Carts.RemoveRange(carts);
                db.SaveChanges();

                return Request.CreateResponse(HttpStatusCode.OK, "Delete Successfully");
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "No Network");
            }
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.