Solved

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

Posted on 2010-11-14
3
620 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

710 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