views:

1304

answers:

4

What is the best way to multiple records in one go in LINQ?

A: 

From the blog post http://solidcoding.blogspot.com/2007/12/sql-in-clause-in-linq.html

      using (DataContext dc = new DataContext("connectionstring"))
   {
     List<string> names = new List<string>();
    names.Add("Anderson");
    names.Add("Johnson");
    List<Person> list = (from p in dc.GetTable<Person>() where names.Contains(p.LastName) select p).ToList();
  }
Tim Hoolihan
The question is to delete records, not select them.
ScottS
A: 

The good old SPROCs.....

You can drag the SPROC to your DBML file and it will generate a rich method in your databasecontext class.

Khurram Aziz
+6  A: 

To delete records with Linq2Sql

CustomerDataContext ctx = new CustomerDataContext("connection string");
Customer cust = ctx.Customers.Where(c => c.Name == "david");

ctx.Customers.DeleteAllOnSubmit(cust);
ctx.SubmitChanges();
David Liddle
This is one of the easiest ways but i would not say is the most efficient. There actually is not any optimization being performed. The generated SQL enumerates all objects that match your query, then manually iterates over them to delete them.See this post - http://stackoverflow.com/questions/869209/bulk-deleting-via-linq
David Liddle
+1  A: 

The following is more for LINQ to Entities, but it may help:

http://stackoverflow.com/questions/869209/bulk-deleting-via-linq

Jagd