Linq to sql 如何转换类型';系统linq.iqueryable<;字节[]>';至';字节[]和#x27;
在LINQtoSQLASP.net中,我想从数据库中获取一个特定的图像并显示在aspx页面上。我在代码中将返回的类型声明为“var”。因此,在获取图像后,它将图像分配给var类型 代码如下:Linq to sql 如何转换类型';系统linq.iqueryable<;字节[]>';至';字节[]和#x27;,linq-to-sql,bytearray,iqueryable,type-conversion,Linq To Sql,Bytearray,Iqueryable,Type Conversion,在LINQtoSQLASP.net中,我想从数据库中获取一个特定的图像并显示在aspx页面上。我在代码中将返回的类型声明为“var”。因此,在获取图像后,它将图像分配给var类型 代码如下: var db = from c in context.Images where c.imageId == iID select c.imageData; return new MemoryStream((by
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[]?您的问题是选择的是
byte[]
的集合,而不是单个byte[]
根据应用程序所需的行为,可以使用Single
、SingleOrDefault
、First
或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);
}
您的问题是选择的是
字节[]
的集合,而不是单个字节[]
根据应用程序所需的行为,可以使用Single
、SingleOrDefault
、First
或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);
}
试着为我做这项工作
var db=(context.Images.FirstOrDefault(i=>i.imageId==ID).ImageData
请在下面一行为我尝试这项工作
var db=(context.Images.FirstOrDefault(i=>i.imageId==ID).ImageData
您应该为linq使用SingleOrDefault选择器,这样您只能获取单个对象而不能获取IQueryable。此外,在SingleOrDefault中,您可以指定类型,这样可以避免强制转换。您应该为linq使用SingleOrDefault选择器,这样您只能获取单个对象而不能获取IQueryable。在SingleOrDefault中,您还可以请指定类型,这样你就可以避免使用castingSorry Justin,你能帮我一下吗?对不起Justin,你能帮我一下吗?