0

I build up stuff that I need into an IEnumerable and then I need to insert the collection into the database, but can't figure how. Please take a look at my code snippet:

public void CopyPonyJetpacks(Pony p_s, Pony p_d)
{
    try
    {
        using (var scope = new TransactionScope())
        {

            var assocs = from x in p_s.Pony_Jetpacks
                         select new Pony_Jetpacks()
                         {
                             Id=Guid.NewGuid(),
                             JetpackId=x.JetpackId,
                             PonyId=p_d.Id
                         };

            data.Pony_Jetpacks.AddObject(assocs); //This doesn't work, what to do?

            data.SaveChanges();
            scope.Complete();
        }
    }
    catch (Exception e)
    {
        throw e;
    }

}

It would be really sad if I had to convert the 'assocs' into list and then insert one by one.

1 Answer 1

1

You don't need to convert your assocs IEnumerable<T> into a List<T> but yes, you need to insert it one by one:

foreach (var assoc in assocs)
    data.Pony_Jetpacks.AddObject(assoc);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.