Converting From System.Data.Linq.Binary to String and Back

Posted by Peter Kellner on November 08, 2009 · 2 mins read

Just a quick post in case anyone is wasting 10 minutes figuring out how to do this.  For me, this came up because in Sql Server 2008, NVARCHAR(MAX) is 4000 characters and I needed more.  The recommended datatype to use for more is VARBINARY.  When I did that, LINQ2SQL converted that type to Linq.Binary. (using encoding)

So, in my C# code, here is what you need to convert to the Linq.Binary:

Linq.Binary carrierMatrix =
   new ASCIIEncoding().GetBytes(myString);

And, to go back from Linq.Binary:
string carrierMatrixString = Encoding.ASCII.GetString(carrierMatrix.ToArray());

Hope this help!