0

following code i used to retrieve Image from database through linq query but i getting error like

LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.

Error line coded with //error occurred.

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

2 Answers 2

1

Looking at the line giving the error;

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

Convert.ToString(user_id) is not an expression that can be used in Linq to Entities.

When looking at your code, I can see that user_id already is a string and doesn't need the conversion, so this should work better;

var category = (from data in db.Register1_db
                where data.User_ID == user_id
                select data.Student_Photo);
Sign up to request clarification or add additional context in comments.

Comments

0

This one should work:

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 creates expressions trees, so Convert.ToString would not get called, but would have to be interpreted by the LINQ provider.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.