convert system.data.linq.binary to byte[]

19,807

Solution 1

Have you tried calling ToArray() on i.data?

var img = from i in db.images
      where i.id == key
      select i.data.ToArray();

System.Data.Linq.Binary has a ToArray method just for that purpose.

Solution 2

Probably its too late by now but may help others :)

//testTable PK:ID, binaryData :binary(32)

public void insertDummyData()
{
    DBML.testTable v = new DBML.testTable ();
    v.ID = 1;

    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
    v.binaryData = new System.Data.Linq.Binary(encoding.GetBytes("11111111000000001111111100000000"));                                                                    

    db.testTable.InsertOnSubmit(v);
    db.SubmitChanges();
}

Or else, Click on the Binary field from .dbml file, open properties and then change the field type from Binary to byte[] as found here

Solution 3

(byte[])linqBinaryField.ToArray()
Share:
19,807

Related videos on Youtube

user1797079
Author by

user1797079

Updated on September 15, 2022

Comments

  • user1797079
    user1797079 over 1 year

    I am storing bytes in a database table. When I retrieve it with Linq 2 sql I get the return type in system.data.linq.Binary.

    I am not able to convert the system.data.linq.binary to byte array(byte[]).

    How do I convert it?

    ///my datacontext
    
    var db = new db();
    
    //key is an value from user
    
    var img = from i in db.images
              where i.id == key
              select i.data; 
    

    the i.data is in linq.binary I want it to be in byte[].

    I tried with (byte[])img but it did not work.

  • user1797079
    user1797079 over 11 years
    nope dude dosent wrk i am using linq to sql to "retrieve" the value and read it into byte array conversion is from System.Data.Linq.Binary to System.byte[]
  • user1797079
    user1797079 over 11 years
    ya it dosent work man it returns System.Data.Linq.Binary[] the array of binary thats it :(
  • user1797079
    user1797079 over 11 years
    plus we hav to use it as var img = (from i in db.images where i.id == key select i.data).ToArray();
  • Mike Two
    Mike Two over 11 years
    The ToArray() call has to be on i.data. You are putting the call after the parentheses. That is going to get you an IEnumerable<System.Data.Linq.Binary[]> and then you call ToArray on that IEnumerable. I'm assuming there can only be one image with that key. Then it looks like you want var img = (from i in db.images where i.id == key select i.data).First().ToArray(); You probably want a null check after First().
  • Jens Kloster
    Jens Kloster almost 11 years
    +1, it worked for others to
  • Cᴏʀʏ
    Cᴏʀʏ over 7 years
    This answer is not useful -- the question asks about converting a System.Data.Linq.Bin‌​ary to a byte[] but this example converts a file path to a byte[] representation, with a Bitmap in between.
  • Bertie
    Bertie about 7 years
    The answer doesn't relate to the OP's question. The answer contains no reference to Linq.Binary type, which is 50% of what the OP is looking for. Please read the question properly before posting an answer.
  • Balagurunathan Marimuthu
    Balagurunathan Marimuthu over 6 years
    Not just posting an answer, you could add little explanation which understand the solution better to OP and future readers as well.
  • janv8000
    janv8000 about 5 years
    Works perfectly, no further explanation needed IMHO
  • Fred
    Fred over 4 years
    The type cast is redundant.