Skip to content

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

Updated: at 02:12 PM

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!

 

Check out the ORM (Object Relational Mapper) PRISMA. The database access method I use in all my projects