Retrieving SQL Type Image Using LINQ

LINQ views the Image type in SQL Server as Binary. So, there are a few options that we have. We can either (a) modify our LINQ to SQL class and change the property to Byte() instead of Binary or we can (b) convert the Binary to an array. To do the latter, you can use a lambda expression to obtain the image you are looking for and grabbing just the image. Here’s an example:

MySqlContext.Photos.Single(Function(p) p.PhotoID = PhotoID).OriginalBytes.ToArray

The above assumes that we have a table called Photos that contains a column called PhotoID. We’ll filter the PhotoID (p.PhotoID) based on our variable PhotoID. Then, we’ll return the Byte Array of the OriginalBytes column to display our image.

Leave a Reply

Your email address will not be published. Required fields are marked *