LINQ-to-SQL bug with stored procedures (sprocs) – must have named columns in returned result sets

This is with 3.5SP1, and unfortunately it took me most of the day to figure out.

While it looks like LINQ-to-SQL (SqlMetal in this case, I assume) supports sprocs that return “raw” (unnamed) columns, naming them Column1 through ColumnN, at runtime they don’t actually get populated with the real data, leaving them always as default(T) for their type.

The easiest way to explain it is to show the repro cases:

Source of the (painfully simple) sprocs:

CREATE PROCEDURE GetRawColumns
AS
    SELECT 1,2,3
GO

CREATE PROCEDURE GetNamedColumns
AS
    SELECT 1 as a,2 as b,3 as c
GO

C# calls to them once they’re added to the dbml (using the UI to drag-drop the sprocs from Server Explorer in this case)

var raw = db.GetRawColumns();
var rawList = raw.ToList();

var named = db.GetNamedColumns();
var namedList = named.ToList();

Actual runtime (in the debugger) values:

image

I haven’t tried mixing named and unnamed columns or anything like that – this blog post represents as much time as I’m willing to put into the bug now that I know a workaround :)

About these ads