Solved

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

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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