简体   繁体   English

使用LINQ在C#中按类将列表中的元素添加到数据库

[英]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. 我使用EntityFramework创建了数据模型,并借助适当的查询从数据库中获取数据。 I would like the button to be able to delete all documents that will be marked as WZ (document number). 我希望该按钮能够删除将被标记为WZ(文档编号)的所有文档。 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. 我创建了HistoryWZ类,它将是一个存储故事的新表,我想使用LINQ查询从一个表中提取数据,将其传递给第二个表,然后从第一个表中删除它。

Using LINQ i got list of data: 使用LINQ,我得到了数据列表:

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. 我想使用创建的类使用LINQ ofc将数据传递到表。

My HistoryWZ class : 我的HistoryWZ课:

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? Table ATable B是否具有相同的结构? 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 . 从您的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 ). Table A查询要提取的项目并将其放在Table B (我们称此Table A Object )。
  3. Create a new Table B Object . 创建一个新的Table B Object
  4. Populate Table B Object with the data from the Table A Object . 使用Table B Object中的数据填充Table B Object Table A Object
  5. Add the Table B Object to the context with DbContext.Add(TableBObject) 使用DbContext.Add(TableBObject)Table B Object添加到上下文中
  6. Save the context changes. 保存上下文更改。
  7. If the changes were successful then use DbContext.Remove(TableAObject) and commit the transaction 如果更改成功,则使用DbContext.Remove(TableAObject)并提交事务
  8. If the changes were not successful then rollback the transaction. 如果更改不成功,则回滚事务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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