Just realize one problem with LINQ to SQL
Unable to serialize the session state. In ‘StateServer’ and ‘SQLServer’ mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in ‘Custom’ mode.
When you have your ASP.NET session state stored in SQL Server, when you put any instance of your LINQ to SQL object into the session, you will hit by the about stated error.
I am still working on a fix for this. Will post the solution here once I found it.
A lot of applications would like to return random results from database. For example an web ad engine that returns random ads from its database based on certain conditions.
To do this in LINQ to SQL you need a bit of programming in T-SQL because LINQ does not have a Random operator nor you can use System.Random in LINQ statements.
Fortunately, LINQ to SQL lets programmers expose their functions and stored procs as methods. So what we can do is create a view first:
CREATE VIEW RandomView
SELECT NEWID() As ID
Then after that you create a function:
CREATE FUNCTION GetNewId
RETURN (SELECT ID FROM RandomView)
In the LINQ to SQL Designer, drag and drop the function into the designer (pic below)
From your LINQ query you can then write something like this
var tools =
from tool in db.Tools
Many thanks to Fabrice for his post here.
Psst… if you looking to learn more about LINQ, ASP.NET 3.5, Silverlight 2 or SQL Server 2008, check out this website where you can download the books for free.
Free downloadable e-book offer- Introducing Microsoft Silverlight 2
Free download- “Introducing SQL Server 2008” e-book offer
During my ILL Lab tag team with Ervin about Linq to SQL, quite a number of attendees asked us about Linq to SQL performance compared to ADO.NET SQLConnection. I couldn’t provide too much information but I remember Rico Mariani’s blog on Linq performance so I asked them to search for it. Search no more, here is the direct link, 5 parts of them about detail study on Linq to SQL performance.
I made a PDF available here for yesterday’s post so that you can have a better view on the source codes.