Solved

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

Posted on 2010-11-14
3
614 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 74

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 74

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now