locked
MVC EF with Linq? RRS feed

  • Question

  • User1901201124 posted

    Hi all, I need some help in how to accomplish this. What I like to do is Select all the Rows in a table that belongs to a certain ID and then Create the selected rows with the exact same data with the exception of making them all with a new ID.

    So let's say my table looks like this.

    Table1
    ------
    ID - Primary Key
    Name
    Title
    JoinID

    So waht I have so far:

    var theRows = from myRows in db.Table1
    .Where(i => i.JoinID == passedInObj.ID)
    select myRows;

    Let's say my rows then looks like this from the above.
    1, "Bob", "Teacher", 5
    2, "Jim", "Janitor", 5

    Now that I have all the Rows that belong to my passedInObj'd id. I like to then insert NEW records back into the database with the exact same info
    But this time I want it with a new JoinID.

    So we should have new records with

    3, "Bob", "Teacher", 10
    4, "Jim", "Janitor", 10

    Thanks!

    Thursday, March 14, 2019 7:38 PM

Answers

  • User1901201124 posted

    Got this to work. Just selected all and used a foreach loop to add it into the database.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 15, 2019 12:33 AM

All replies

  • User-474980206 posted

    why wouldn't this be a set operation:

       insert table1 (name, title, joinID)
       select name, title, joinID
       from table1
       where joinId = @passedID

    Thursday, March 14, 2019 8:17 PM
  • User1901201124 posted

    That won't get me to the end state. I need a copy of the record with a new JoinID.

    I noticed that i miss typed the original question - has been updated.

    Thursday, March 14, 2019 8:29 PM
  • User753101303 posted

    Hi,

    And this new JoinID is created how? It might be just an additional parameter :

    nsert table1 (name, title, joinID)
       select name, title, @newJoinID
       from table1
       where joinId = @passedID

    Also it looks weird to "copy" peoples. Not sure what is the exact use case in plain English. Don't you want to "attach" the same people to another "JoinID" ?

    Thursday, March 14, 2019 9:50 PM
  • User1901201124 posted

    So basically Table1 is a join table. The parent, say Table0 has a 0,1 to many relationship with Table1.

    What I am doing is I will create a new record atTable0 ( and say it generated PK of 10 in the original question) this new ID will be the "joinId". I need to make a "copy" of all the child records and point it to this newly created record with ID 5.

    Thursday, March 14, 2019 10:08 PM
  • User1901201124 posted

    Got this to work. Just selected all and used a foreach loop to add it into the database.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 15, 2019 12:33 AM
  • User-474980206 posted

    using a for loop is a pretty bad design, this should be done as a set operation.

    Friday, March 15, 2019 4:14 AM
  • User1901201124 posted

    Yeah probably but because its such a small function which won't be used heavily i need to move on to the next part of the programming. It works for now and i'll go back to optimize when I have more time. Thanks

    Friday, March 15, 2019 2:25 PM