Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

896 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

14 Experts available now in Live!

Get 1:1 Help Now