how to update the multiple rows at a time using linq to sql?

To update one column here are some syntax options: Option 1 var ls=new int[]{2,3,4}; using (var db=new SomeDatabaseContext()) { var some= db.SomeTable.Where(x=>ls.Contains(x.friendid)).ToList(); some.ForEach(a=>a.status=true); db.SubmitChanges(); } Option 2 using (var db=new SomeDatabaseContext()) { db.SomeTable .Where(x=>ls.Contains(x.friendid)) .ToList() .ForEach(a=>a.status=true); db.SubmitChanges(); } Option 3 using (var db=new SomeDatabaseContext()) { foreach (var some in db.SomeTable.Where(x=>ls.Contains(x.friendid)).ToList()) { some.status=true; } db.SubmitChanges(); } … Read more

Random row from Linq to Sql

You can do this at the database, by using a fake UDF; in a partial class, add a method to the data context: partial class MyDataContext { [Function(Name=”NEWID”, IsComposable=true)] public Guid Random() { // to prove not used by our C# code… throw new NotImplementedException(); } } Then just order by ctx.Random(); this will do … Read more

NHibernate vs LINQ to SQL

LINQ to SQL forces you to use the table-per-class pattern. The benefits of using this pattern are that it’s quick and easy to implement and it takes very little effort to get your domain running based on an existing database structure. For simple applications, this is perfectly acceptable (and oftentimes even preferable), but for more … Read more

LINQ to SQL Left Outer Join

You don’t need the into statements: var query = from customer in dc.Customers from order in dc.Orders .Where(o => customer.CustomerId == o.CustomerId) .DefaultIfEmpty() select new { Customer = customer, Order = order } //Order will be null if the left join is null And yes, the query above does indeed create a LEFT OUTER join. … Read more