none
System.Data.Linq.Binary[] to byte[] RRS feed

  • Question

  • Learning LINQ to SQL, not very good at SQL either, learning it also. I am trying to retrieve an image data type from my test data base using LINQ to SQL. I need the value from the database to be in a byte[] for some WPF code to work.

     

    If I open a connection and use a reader and an index I can get a byte[], no problem works fine;

     

    byte[]image = reader.GetValue(3) as byte[];   // index to first image

     

    If I use LINQ to SQL I can get a Binary[], but can’t convert to byte[]. If I put in a breakpoint, the Binary[] pic appears to be the correct size. I have tried various conversions, none work all have errors, some compile and blow out at run time, see below. Is it possible in LINQ to do this?

     

    I have searched, can't find anything that works. Any help would be appreciated,

     

    Dave

     

     

           private DataClasses1DataContext testConnect = new DataClasses1DataContext();

     

            public byte[] GetImage(string name)

            {

                var img = from p in testConnect.TestImages

                            where p.imageName.Contains("test.jpg")

                            select p.image;

     

                Binary[] pic = img.ToArray();    // convert

                byte[] picArray = pic.Cast<byte>().ToArray();    // convert

                //byte[] picArray = pic.ToArray();

                //byte[] picArray = new byte[pic.Length];

                //pic.CopyTo(picArray, 0);

                return picArray; // return byte array

            }

    Saturday, June 20, 2009 10:19 PM

Answers

  • Your query returns a sequence of images that contains "test.jpg". By calling ToArray() on it, you're converting this sequence into an array - you're not actually calling ToArray on the binary column itself.

    Intead, you want to call First() or Single() to get a single matching element, then you can call ToArray() on the binary column type to get the bytes:

     
    public byte[] GetImage(string name)

      {

         var db = new DataClasses1DataContext();

         var images = from p in testConnect.TestImages

                               where p.imageName.Contains("test.jpg")

                               select p.image;

     

         return images.First(). ToArray(); 
       }

     

     

    You can expand the last line to this to make it clearer:

     

       Binary pic = images.First();

       byte[] data = pic.ToArray();

       return data;

     

    Joe


    Write LINQ queries interactively - www.linqpad.net
    • Marked as answer by davgreen Sunday, June 21, 2009 10:26 AM
    Sunday, June 21, 2009 1:47 AM
    Answerer

All replies

  • Your query returns a sequence of images that contains "test.jpg". By calling ToArray() on it, you're converting this sequence into an array - you're not actually calling ToArray on the binary column itself.

    Intead, you want to call First() or Single() to get a single matching element, then you can call ToArray() on the binary column type to get the bytes:

     
    public byte[] GetImage(string name)

      {

         var db = new DataClasses1DataContext();

         var images = from p in testConnect.TestImages

                               where p.imageName.Contains("test.jpg")

                               select p.image;

     

         return images.First(). ToArray(); 
       }

     

     

    You can expand the last line to this to make it clearer:

     

       Binary pic = images.First();

       byte[] data = pic.ToArray();

       return data;

     

    Joe


    Write LINQ queries interactively - www.linqpad.net
    • Marked as answer by davgreen Sunday, June 21, 2009 10:26 AM
    Sunday, June 21, 2009 1:47 AM
    Answerer
  • Joe,

     

    Thanks for the help, fixed the problem. I really appreciate your quick response. I made some wrong assumptions on how this worked. Probably won’t be the last time as I learn LINQ to SQL and SQL.

     

    Dave

    Sunday, June 21, 2009 10:28 AM