Solved

Linq to sql - Don't understand why I get error from example

Posted on 2010-11-14
3
619 Views
Last Modified: 2013-11-12
I have only been playing with asp.net and linq for about a year and am trying to create a picture portfolio.
I found an example on the web, but am having a problem understanding the function provided.
In the sample app, they receive no error, but in my environment, I recieve an error
value of type system.data.linq.binary cannot be converted to 1 - dimensional array of byte.
The fields in the db are of type image.
Would someone please explain to me what this is doing and why I am getting the error when it table is set up like it.
Thanks

Private Shared Function GetPhotoBytes(ByVal source As IQueryable(Of Portfolio), ByVal size As PhotoSize) As Stream
        Dim imageBytes As Byte()

        Select Case size
            Case PhotoSize.Large
                imageBytes = source.Select(Function(p) p.BeforePicBytesFull).SingleOrDefault
            Case PhotoSize.Original
                imageBytes = source.Select(Function(p) p.BytesOriginal).SingleOrDefault
            Case PhotoSize.Small
                imageBytes = source.Select(Function(p) p.BytesThumb).SingleOrDefault
            Case Else
                imageBytes = source.Select(Function(p) p.BytesPoster).SingleOrDefault
        End Select
        If imageBytes IsNot Nothing Then
            Return New MemoryStream(imageBytes)
        Else
            Return New MemoryStream()
        End If
    End Function

Open in new window

0
Comment
Question by:Sheritlw
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 34133548
Select() is a transforming function. In other words, you take one type of object and convert it to another type. Your code is trying to assign a type of System.Data.Linq.Binary to an object of type Byte(). You may try calling the ToArray() method on your objects in your Selects:
Private Shared Function GetPhotoBytes(ByVal source As IQueryable(Of Portfolio), ByVal size As PhotoSize) As Stream
        Dim imageBytes As Byte()

        Select Case size
            Case PhotoSize.Large
                imageBytes = source.Select(Function(p) p.BeforePicBytesFull.ToArray()).SingleOrDefault
            Case PhotoSize.Original
                imageBytes = source.Select(Function(p) p.BytesOriginal.ToArray()).SingleOrDefault
            Case PhotoSize.Small
                imageBytes = source.Select(Function(p) p.BytesThumb.ToArray()).SingleOrDefault
            Case Else
                imageBytes = source.Select(Function(p) p.BytesPoster.ToArray()).SingleOrDefault
        End Select
        If imageBytes IsNot Nothing Then
            Return New MemoryStream(imageBytes)
        Else
            Return New MemoryStream()
        End If
    End Function

Open in new window

0
 

Author Comment

by:Sheritlw
ID: 34133625
That seems to have worked.
Thank you
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34133702
Sure thing. Glad to help  :)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Programming  of  forecasting lottery numbers for self use  on Turbo C++ is done by use of random number generator code by a program algorithm logic. Random numbers usually generates numbers between definite interval. Therefore, when entering the c…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question