Updating a single record in forEach using C#/Entity

Is there any way I can update a record after doing something in a for each? This is the idea (in bold), but doesn't work.

 using (var db = new PTO_SystemEntities())
            {
                var pto = db.PTO_REQUEST
                    .Where(q => q.Status != "Cancelled" && q.Status != "Processed" && q.Type != "Reverse Leave");
                foreach (var i in pto)
                {
                    Console.Write(i.EMPLID.Trim() + ", " + location.Trim() + ", " + jobcode.Trim() + ", " + i.Hours + Environment.NewLine);
[b]                    db.Entry(i).Property(e => e.Status).CurrentValue = "Processed";
                    db.Entry(i).Property(e => e.Status).IsModified = true;
                    db.SaveChanges();[/b]
                }
            }

Open in new window

Mike MillerSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi Mike;

You state the following, "This is the idea (in bold), but doesn't work.", how does it not work, do you get a run time error or compile error or something else?

Please also post the schema of the PTO_REQUEST table from the database.
Mike MillerSoftware EngineerAuthor Commented:
This is the error I'm getting:

"New transaction is not allowed because there are other threads running in the session."

Open in new window

Fernando SotoRetiredCommented:
Please post the schema of the database table PTO_REQUEST. Thanks
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Mike MillerSoftware EngineerAuthor Commented:
ID int (primary key, is identity)
Status VarChar (50)
Hours Decimal (5, 2)
EMPLID char(11)
Fernando SotoRetiredCommented:
Hi Mike;

OK, the issue is that you have a active reader pulling in data from the query and at the same time trying to update the values to the database in your foreach loop. To correct the issue use the ToList query method to the query as follows
var pto = db.PTO_REQUEST
            .Where(q => q.Status != "Cancelled" && q.Status != "Processed" && q.Type != "Reverse Leave").ToList();

Open in new window

This will cause the query to execute before processing the data.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike MillerSoftware EngineerAuthor Commented:
Yup, was just coming on to post my solution...

            using (var db = new PTO_SystemEntities())
            {
                var pos = db.PTO_REQUEST
                    .Where(q => q.Status != "Cancelled" && q.Status != "Processed" && q.Type != "Reverse Leave");
                foreach (var i in pos)
                {
		    //whatever code here
                    using (var dd = new PTO_SystemEntities())
                    {
                        dd.PTO_REQUEST
                            .Where(q => q.ID == i.ID)
                            .ToList()
                            .ForEach(a => a.Status = "Processed");
                        dd.SaveChanges();
                    }                                       
                }}

Open in new window

Fernando SotoRetiredCommented:
Very good Mike, have a great day.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.