Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9234955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:54:41+00:00 2026-06-18T06:54:41+00:00

I need to pull any amount of records that correspond to a specific value

  • 0

I need to pull any amount of records that correspond to a specific value (CourseCode), and insert these records into another table. This code works fine as long as the Linq code returns only one record, however if there is any more than that I get the following message:

An object with the same key already exists in the ObjectStateManager.
The existing object is in the Modified state. An object can only be
added to the ObjectStateManager again if it is in the added.

Below is my code:

            if (_db == null) _db = new AgentResourcesEntities();

            var prodCodes = from records in _db.CourseToProduct
                              where records.CourseCode == course.CourseCode
                              select records;

            foreach (var pt in prodCodes.ToList())
            {
                agentProdTraining.SymNumber = symNumber;
                agentProdTraining.CourseCode = course.CourseCode;
                agentProdTraining.ProductCode = pt.ProductCode;
                agentProdTraining.DateTaken = course.DateTaken;
                agentProdTraining.Method = course.Method;
                agentProdTraining.LastChangeOperator = requestor;
                agentProdTraining.LastChangeDate = DateTime.Now;
                agentProdTraining.DateExpired = course.ExpirationDate;
                agentProdTraining.ProductCode = pt.ProductCode;
                agentProdTraining.NoteId = pt.NoteId;

                _db.AgentProductTraining.AddObject(agentProdTraining);
                _db.SaveChanges();

                PtAdded++;

                EventLog.WriteEntry(sSource, "Product Training added", EventLogEntryType.Warning);
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:54:42+00:00Added an answer on June 18, 2026 at 6:54 am

    The loop is re-adding the same agentProdTraining object even though property values are changed. Create a new instance for each loop execution.

    foreach (var pt in prodCodes.ToList())
    {
      var agentProdTraining = new AgentProductTraining();
    
      agentProdTraining.SymNumber = symNumber;
      agentProdTraining.CourseCode = course.CourseCode;
      agentProdTraining.ProductCode = pt.ProductCode;
      agentProdTraining.DateTaken = course.DateTaken;
      agentProdTraining.Method = course.Method;
      agentProdTraining.LastChangeOperator = requestor;
      agentProdTraining.LastChangeDate = DateTime.Now;
      agentProdTraining.DateExpired = course.ExpirationDate;
      agentProdTraining.ProductCode = pt.ProductCode;
      agentProdTraining.NoteId = pt.NoteId;
    
      _db.AgentProductTraining.AddObject(agentProdTraining);
      _db.SaveChanges();
    
      PtAdded++;
    
      EventLog.WriteEntry(sSource, "Product Training added", EventLogEntryType.Warning);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to pull all the links for a page that resides on an
I need to pull data from one SQL 2005 Express database to another and
From a string, I need to pull out groups that match a given pattern.
I need to pull a large amount of data from various tables across a
I have a large set of real-world text that I need to pull words
I need to pull back from a database JSON documents that are not based
From a string I need to pull out any word beginning with dog. Eg.
So this is driving me nuts. I need to pull my table rows from
I need to pull all non-private items from a user's calendar in Exchange 2003.
I need to pull a list of items from a MySQL database and then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.