0

以下代码我曾经通过 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;
    }
}
4

2 回答 2

1

查看给出错误的行;

var category = (from data in db.Register1_db
                where data.User_ID == Convert.ToString(user_id)
                select data.Student_Photo);

Convert.ToString(user_id)不是可以在 Linq to Entities 中使用的表达式。

查看您的代码时,我可以看到它user_id已经是一个字符串并且不需要转换,所以这应该会更好;

var category = (from data in db.Register1_db
                where data.User_ID == user_id
                select data.Student_Photo);
于 2012-10-06T14:56:32.640 回答
0

这个应该工作:

var string_id = Convert.ToString(user_id);
var category = (from data in db.Register1_db
                       where data.User_ID == string_id
                       select data.Student_Photo);

LINQ 创建表达式树,因此Convert.ToString不会被调用,但必须由 LINQ 提供程序解释。

于 2012-10-06T14:57:19.580 回答