如何转换类型'system.linq.iqueryable '到'byte []'

问题描述 投票:0回答:2

在Linq to SQL asp.net中,我想从数据库中获取特定图像并在aspx页面上显示。我在代码中将返回的类型声明为“var”。因此,在获取图像之后,它将图像分配给var类型。

这是代码:

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

它给出了这个编译错误:无法将类型'system.linq.iqueryable'转换为'byte []'。

如何将'system.linq.iqueryable类型转换为byte []?

linq-to-sql bytearray iqueryable type-conversion
2个回答
3
投票

你的问题是你选择的是byte[]而不是单个byte[]的集合。

根据您的应用程序所需的行为,您可以使用SingleSingleOrDefaultFirstFirstOrDeault

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);
}

1
投票

尝试下面这项工作对我来说

var db = (context.Images.FirstOrDefault(i => i.imageId == ID).ImageData

© www.soinside.com 2019 - 2024. All rights reserved.