首页 > 解决方案 > 使用 LINQ 和实体框架将列表元素添加到数据库

问题描述

在创建应用程序时,我遇到了一个问题。我创建了一个数据模型,其中有 2 个类(还有更多,但在示例中是关于这 2 个)

第一类涉及 WZ 文档:

public partial class WZ_DWS_SITO
{
    public string WZ { get; set; }
    public string WZ_DATA { get; set; }
    public string WZ_CZAS { get; set; }
    public string WZ_POZYCJA { get; set; }
    public string MATERIAL { get; set; }
    public string PARTIA { get; set; }
    public string ZAKLAD { get; set; }
    public string SKLAD { get; set; }
    public decimal WZ_ILOSC { get; set; }
    public string ZLECENIE { get; set; }
    public string ZLECENIE_DATA_UTWORZENIA { get; set; }
    public string KUNNR { get; set; }
    public string ZLECENIE_POZYCJA { get; set; }
    public decimal ZLECENIE_CENA_NETTO { get; set; }
    public decimal ZLECENIE_ILOSC { get; set; }
    public decimal ZLECENIE_NETTO { get; set; }
    public decimal ZLECENIE_KOSZT { get; set; }
    public string WWW { get; set; }
    public Nullable<decimal> ZLECENIE_CENA_ZAKUPU { get; set; }
    public Nullable<decimal> ZLECENIE_ZYSK { get; set; }
    public Nullable<decimal> ZLECENIE_MARZA { get; set; }
    public string VE { get; set; }
    public Nullable<decimal> WZ_KOSZT { get; set; }
    public Nullable<decimal> WZ_NETTO { get; set; }
    public Nullable<decimal> WZ_ZYSK { get; set; }
    public Nullable<decimal> WZ_CALA_NETTO { get; set; }
    public Nullable<decimal> ATP_SKLAD { get; set; }
    public Nullable<decimal> ATP_ZAKLAD { get; set; }
    public string DATA_WYCENY { get; set; }
    public Nullable<decimal> CENA_MINIMALNA { get; set; }
    public Nullable<decimal> WZ_ILOSC_NA_WZ { get; set; }
    public Nullable<decimal> ILOSC_60DNI_ABC_5PROCENT { get; set; }
    public string PRZYCZYNA { get; set; }
    public string STATUS { get; set; }
    public string L0 { get; set; }
    public string L1 { get; set; }
    public string L2 { get; set; }
    public string NAZWA_KLIENTA { get; set; }
    public Nullable<int> INDEKS { get; set; }
    public string NAZWA_MATERIALU { get; set; }
    public string PH { get; set; }
    public Nullable<decimal> MARZA_ZLECENIE_ALL { get; set; }
}

第二个收集接受文件的历史:

public class HISTORYWZ
{
    public string NrWZ { get; set; }
    public string ODBIORCA { get; set; }
    public string DATAWZ { get; set; }

    public int 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; }

}

Chciałbym wyciągając pojedyńczą WZ (która może mieć kilka pozycji) móc wrzucić ją do historii ale tylko elementy które są mi potrzebne。Wyciągnąłem sobie elementy za pomocą 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 
                    ).ToList();

我想通过将 WZ_DWS_SITO 类中的 WZ 对应于 HistoryWZ 类中的 WZ 编号等来将这些元素添加到数据库中。然后我想从数据库中删除记录,但我已经覆盖了自己。

标签: c#entity-frameworklinq

解决方案


var skad = (from d in ent.WZ_DWS_SITO
            where d.WZ == actwuzetka
            select new HISTORYWZ()
            {
                //NrWZ = d.WZ_ ,
                //ODBIORCA = d.ODBIORCA,
                //DATAWZ = d.DATAWZ,
                INDEKS = d.INDEKS.Value,
                MATERIAL = d.MATERIAL,
                PARTIA = d.PARTIA,
                ILOSC = d.WZ_ILOSC,
                PRZYCZYNA = d.PRZYCZYNA,
                NAZWA_MATERIALU = d.NAZWA_MATERIALU,
                //CENAMIN = d.WZ_ICENAMIN,
                //CENASPRZ = d.WZ_ICENASPRZ,
                //VPRS = d.WZ_VPRS,
                //MARZA = d.WZ_MARZA,
            }
            ).ToList();

推荐阅读