Reputation: 401
In Linq to SQL asp.net, I want to fetch a specific image from database and display on an aspx page. I declared returned type as "var" on the code. So after fetching the image, it assigns image to var type.
Here is the code:
var db = from c in context.Images
where c.imageId == iID
select c.imageData;
return new MemoryStream((byte[])db); ---->> this line of code gives error
It gives this compile error: Cannot convert type 'system.linq.iqueryable' to 'byte[]'.
How can I convert 'system.linq.iqueryable type to byte[] ?
Upvotes: 0
Views: 2977
Reputation: 624
try below line this work for me
var db = (context.Images.FirstOrDefault(i => i.imageId == ID).ImageData
Upvotes: 1
Reputation: 245479
Your problem is that you're selecting a collection of byte[]
rather than a single byte[]
.
Depending on the behavior you want for your application, you can use Single
, SingleOrDefault
, First
, or FirstOrDeault
:
var image = context.Images.FirstOrDefault(i => i.imageId == ID);
if(image == null)
{
// Handle the case where no image was found.
//
// First or Single would throw Exceptions in this case
// if that's what you want.
}
else
{
return new MemoryStream(image.imageData);
}
Upvotes: 3