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()
Related videos on Youtube
Author by
user1797079
Updated on September 15, 2022Comments
-
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 inlinq.binary
I want it to be inbyte[]
.I tried with
(byte[])img
but it did not work. -
user1797079 over 11 yearsnope 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 over 11 yearsya it dosent work man it returns System.Data.Linq.Binary[] the array of binary thats it :(
-
user1797079 over 11 yearsplus we hav to use it as
var img = (from i in db.images where i.id == key select i.data).ToArray();
-
Mike Two over 11 yearsThe
ToArray()
call has to be oni.data
. You are putting the call after the parentheses. That is going to get you anIEnumerable<System.Data.Linq.Binary[]>
and then you callToArray
on thatIEnumerable
. I'm assuming there can only be one image with that key. Then it looks like you wantvar img = (from i in db.images where i.id == key select i.data).First().ToArray();
You probably want a null check afterFirst()
. -
Jens Kloster almost 11 years+1, it worked for others to
-
Cᴏʀʏ over 7 yearsThis answer is not useful -- the question asks about converting a
System.Data.Linq.Binary
to abyte[]
but this example converts a file path to abyte[]
representation, with aBitmap
in between. -
Bertie about 7 yearsThe 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 over 6 yearsNot just posting an answer, you could add little explanation which understand the solution better to OP and future readers as well.
-
janv8000 about 5 yearsWorks perfectly, no further explanation needed IMHO
-
Fred over 4 yearsThe type cast is redundant.