在代码优先的实体框架中插入/ sql等效的sql代码

时间:2013-04-16 05:13:27

标签: linq entity-framework c#-4.0 entity-framework-4.1 entity-framework-5

等同于代码优先的实体框架中的代码

INSERT INTO TableA (a,b)
SELECT a,b
FROM TableB
WHERE a=10

1 个答案:

答案 0 :(得分:0)

我相信您尝试根据条件从TableB中选择几列,然后将所有这些对象添加到TableA,可以执行以下操作:

//Create list of TableA objects after selecting and projecting from TableB
var list = db.TableB.Where(t => t.a == 10)
                    .Select(r => new TableA() { A = r.a, B = r.b })
                    .ToList();
//Add those items to TableA through DataContext
foreach (var item in list)
    db.TableA.AddObject(item);
//Call Save
db.SaveChanges();