Patrick’s Bytes

Techie for the past 30 years…

LINQ to SQL and SQL Server Session State – Unable to serialize the session state

Just realize one problem with LINQ to SQL

image

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.

Random results from LINQ to SQL

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
AS
SELECT NEWID() As ID

Then after that you create a function:

CREATE FUNCTION GetNewId
(
)
RETURNS uniqueidentifier
AS
BEGIN
RETURN (SELECT ID FROM RandomView)
END

In the LINQ to SQL Designer, drag and drop the function into the designer (pic below)

image

From your LINQ query you can then write something like this

var tools =
  from tool in db.Tools
  orderby db.GetNewId()
  select tool.Name;

Many thanks to Fabrice for his post here.

http://weblogs.asp.net/fmarguerie/archive/2008/01/10/randomizing-linq-to-sql-queries.aspx

Free MS Press books

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. :)

ms_asp_net_cvrms_linq_cvr ms_silverlight_cvr

Free downloadable e-book offer- Introducing Microsoft Silverlight 2

ms_sql_srv_cvr

Free download- “Introducing SQL Server 2008” e-book offer

Linq to SQL vs ADO.NET Performance

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.

PDF on OpenXML Doc Gen walkthrough

I made a PDF available here for yesterday’s post so that you can have a better view on the source codes.

Older Posts »