简体   繁体   中英

Add element from list to database by class in c# using LINQ

I created a data model using EntityFramework and with the help of appropriate queries I get the data from the database. I would like the button to be able to delete all documents that will be marked as WZ (document number). I created the HistoryWZ class which will be a new table storing the story and I would like to use the LINQ query to extract data from one table, pass it to the second table and delete it from the first table.

Using LINQ i got list of data:

EntitiesSito ent = new EntitiesSito();
        dynamic wkaa = datagridview.SelectedItem;
        string actwuzetka = wkaa.WZ.ToString();

        var skad = (from d in ent.WZ_DWS_SITO
                    where d.WZ == actwuzetka
                    select new
                    {
                        d.WZ,
                        d.KUNNR
                    }).ToList();

I would like to use the created class to pass data to the table using LINQ ofc.

My HistoryWZ class :

ublic class HistoryWZ
{
    public string NrWZ { get; set; }
    public string ODBIORCA { get; set; }
    public string DataWZ { get; set; }
    public string INDEKS { get; set; }
    public string MATERIAL { get; set; }
    public string PARTIA { get; set; }
    public decimal ILOSC { get; set; }
    public string NAZWA_MATERIALU { get; set; }
    public string PRZYCZYNA { get; set; }
    public decimal CENAMIN{ get; set; }
    public decimal CENASPRZ { get; set; }
    public decimal VPRS { get; set; }
    public decimal MARZA { get; set; }

    public string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;


}

Are Table A and Table B of the same structure? If so, it sounds like you've got a problem at the database schema level as its not properly normalised. If they are not, then:

  1. Create a new transaction from your DbContext .
  2. Query for the item you want to take from Table A and place in Table B (We will call this Table A Object ).
  3. Create a new Table B Object .
  4. Populate Table B Object with the data from the Table A Object .
  5. Add the Table B Object to the context with DbContext.Add(TableBObject)
  6. Save the context changes.
  7. If the changes were successful then use DbContext.Remove(TableAObject) and commit the transaction
  8. If the changes were not successful then rollback the transaction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM