1

我正在从一个表中获取一个项目并插入到具有相同表结构的另一个表中如何使用LINQto entity in C#

public static void GetInfillIDAndInsertIntoInfillPO(int infillID)
{
    Entities db = new Entities();
    List<Infill> infillitem = (from c in db.Infills
                                    where (c.InfillID == infillID)
                                    select c).ToList();

    List<InfillPO> infillPO = new List<InfillPO>();

    db.InfillPOes.Add(infillitem);

}

这里InfillInfillPO具有相同的表结构。只是InfillPO不包含Primary key。现在,我只想插入InfillPO表中的记录。

我也通过铸造尝试过。但不会工作。

public static void GetInfillIDAndInsertIntoInfillPO(int infillID)
{
    XtremeProcurementEntities db = new XtremeProcurementEntities();
    Infill infillitem = (from c in db.Infills
                                where (c.InfillID == infillID)
                                select c).FirstOrDefault();

    InfillPO infillPO = new InfillPO();

    db.InfillPOes.Add((infillPO)infillitem);                
}

帮助表示赞赏!

4

1 回答 1

0

您从表中选择类型 T1 并将其选择到 T2 中。然后您可以将此 T2 添加到上下文中并保存。

List<InfillPO> infillitem = (from c in db.Infills
                                       where (c.InfillID == infillID)
                                               select new InfillPO
                             {
                                  PropertyA = c.PropertyA
                             }).ToList();

这将是最简单和最快的解决方案。取决于您是否要经常更改列...

于 2013-05-27T12:59:58.607 回答