-1

What I am trying to create is when a user select an item, that item will disappear from the list of items. Some items can be submitted once and once it is submitted, the user not be able to submit the same item again. Submitted items will be logged to the database.

The issue I am having is figuring out what is wrong with my logic here as it is breaking and what can I do to improve this?

using (var db = new myDatabase())
        {
            var itemLists = db.GetAllItem().ToList();
            var userSubmittedItems = db.GetAllUserItem("LoginID").ToList();

            if (userSubmittedItems.Count > 0)
            {
                foreach (var submittedItems in userSubmittedItems)
                {
                    foreach (var item in itemLists)
                    {
                        int itemID = item.ItemID;
                        int userItemID = userSubmittedItems.UserItemID;

                        if (itemID == userItemID && item.OneTime == true)
                        {
                            itemLists.Remove(item);
                        }
                    }
                }
            }
Lukkha Coder
  • 4,421
  • 29
  • 25
FTao
  • 11
  • 4
  • you're only removing from a collection in `itemLists`, if you want to delete them from the database as well you will need to do `db.ItemEntity.Remove(item)` and end with `db.SaveChanges()` to commit the changes to the database. – balexandre Dec 01 '15 at 22:38
  • *it is breaking* doesn't say much. – Gert Arnold Dec 02 '15 at 12:18

1 Answers1

0

you're only removing the items in your collection itemLists, you are not performing anything in the database it self... for that you should, and imagining that your Entity for the Items is called ItemEntity do this:

    using (var db = new myDatabase())
    {
        var itemLists = db.GetAllItem().ToList();
        var userSubmittedItems = db.GetAllUserItem("LoginID").ToList();

        if (userSubmittedItems.Count > 0)
        {
            foreach (var submittedItems in userSubmittedItems)
            {
                foreach (var item in itemLists)
                {
                    int itemID = item.ItemID;
                    int userItemID = userSubmittedItems.UserItemID;

                    if (itemID == userItemID && item.OneTime == true)
                    {
                        itemLists.Remove(item);
                        db.ItemEntity.Remove(item); // mark for delete
                    }
                }
            }
            db.SaveChanges(); // all marked items, if any, will now 
                              //  be committed in a db call
        }

more on removing records with EF: Delete a single record from Entity Framework?

Community
  • 1
  • 1
balexandre
  • 73,608
  • 45
  • 233
  • 342
  • The all item list will be static, what I was hoping for is to modify the existing list to what hasn't been submitted then display that information base on what user is logged in. I have tried using two variables but it ended up changing both variable for some reason. One would be the original and the other modified to be displayed. – FTao Dec 01 '15 at 23:30
  • I get this: Collection was modified; enumeration operation may not execute. Even if I am using two different variables. Not sure if I am overthinking this or if I am missing something. – FTao Dec 01 '15 at 23:35