以下代码我曾经通过 linq 查询从数据库中检索图像,但我得到了类似的错误
LINQ to Entities 无法识别方法“System.String ToString(System.String)”方法,并且该方法无法转换为存储表达式。
用 //error 编码的错误行发生了。
public void ProcessRequest (HttpContext context) {
HttpRequest req = context.Request;
MembershipUser user_id1 = Membership.GetUser();
string user_id = user_id1.ProviderUserKey.ToString();
// string categoryID =Convert.ToInt32("4");// req.QueryString["CategoryID"].ToString();
// Get information about the specified category
TMSEntities db = new TMSEntities();
//get User ID From Login
var category = (from data in db.Register1_db
where data.User_ID == Convert.ToString(user_id)
select data.Student_Photo);
//error occurred.
int len = category.First().Length;
// Output the binary data
// But first we need to strip out the OLE header
int OleHeaderLength = 78;
int strippedImageLength = len - OleHeaderLength;
byte[] imagdata = new byte[strippedImageLength];
Array.Copy(category.First().ToArray(), OleHeaderLength, imagdata, 0, strippedImageLength);
if ((imagdata) != null)
{
MemoryStream m = new MemoryStream(imagdata);
System.Drawing.Image image = System.Drawing.Image.FromStream(m);
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
public bool IsReusable {
get {
return false;
}
}