Multiple/single instance of Linq to SQL DataContext

Rick Strahl has a nice article about your options: http://www.west-wind.com/weblog/posts/246222.aspx. See also: LINQ to SQL – where does your DataContext live?. You may want a slightly different strategy for each type of deployment – web, desktop, windows service… Summarized, your options are: Global DataContext – dangerous in multi-threaded environments (including web apps). Remember that instance … Read more

List, IList, IEnumerable, IQueryable, ICollection, which is most flexible return type?

Collections are not generally very useful for DAL returns, because a collection does not implicitly guarantee order. It’s just a bucket of items. An IList, on the other hand, does implicitly guarantee order. So we’re down to IEnumerable or IList. The next question would be: is the List object “live”? i.e., is it connected to … Read more

How do I convert multiple inner joins in SQL to LINQ?

Using query syntax: from c in dbo.Companies join p in dbo.Persons on c.AccountCoordinatorPersonId equals p.PersonId join p2 in dbo.Persons on c.AccountManagerPersonId equals p2.PersonId select new { c.CompanyId, c.CompanyName, AccountCoordinator = p.FirstName + ‘ ‘ + p.Surname, AccountManager = p2.FirstName + ‘ ‘ + p2.Surname } Using method chaining: dbo.Companies.Join(dbo.Persons, c => c.AccountCoordinatorPersonId, p => p.PersonId, … Read more

Best way to update LINQ to SQL classes after database schema change

You can use SQLMetal.exe to generate your dbml and or cs/vb file. Use a pre-build script to start it and target the directory where your datacontext project belongs. C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64\sqlmetal.exe /server:<SERVER> /database:<database> /code:”path\Solution\DataContextProject\dbContext.cs” /language:csharp /namespace:<your namespace>

Multiple WHERE clause in Linq

Well, you can just put multiple “where” clauses in directly, but I don’t think you want to. Multiple “where” clauses ends up with a more restrictive filter – I think you want a less restrictive one. I think you really want: DataTable tempData = (DataTable)grdUsageRecords.DataSource; var query = from r in tempData.AsEnumerable() where r.Field<string>(“UserName”) != … Read more

What can I do to resolve a “Row not found or changed” Exception in LINQ to SQL on a SQL Server Compact Edition Database?

Thats nasty, but simple: Check if the data types for all fields in the O/R-Designer match the data types in your SQL table. Double check for nullable! A column should be either nullable in both the O/R-Designer and SQL, or not nullable in both. For example, a NVARCHAR column “title” is marked as NULLable in … Read more

tech