views:

55

answers:

2

Is there a way to do an insert/select with Linq that translates to this sql:

INSERT INTO TableA (...)
SELECT ...
FROM TableB
WHERE ...
+2  A: 
context
  .TableA
  .InsertAllOnSubmit(
    context
      .TableB
      .Where( ... )
      .Select(b => new A { ... })
  );
bzlm
+1  A: 

Yes @bzlm covered it first, but if you prefer something a bit more verbose:

// dc = DataContext, assumes TableA contains items of type A
var toInsert = from b in TableB
               where ...
               select new A 
               {
                   ...
               };

TableA.InsertAllOnSubmit(toInsert);
dc.SubmitChanges();

I kind of prefer this from a review/maintenance point of view as I think its a bit more obvious what's going on in the select.

Murph