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

前端 未结 3 1985
不思量自难忘°
不思量自难忘° 2020-11-27 23:47

I am using Entity Framework, and I have a line of code that is taking a var and translating it back to an iint for the database.

var record = context.enrollm         


        
相关标签:
3条回答
  • 2020-11-27 23:56

    in Linq to Entity, you should use the methods in your query which is supported by your provider to convert them to expression tree to run on your Data Base side.

    all providers must support some methods by default called Canonical Functions (Read More Here), and also you can define your user defined function and stored procedure as edm functions to use in linq query (Read More Here) and (Here).

    in addition you can use methods which is supported by providers and can be converted to expression tree which are in EntityFunctions and SqlFunctions.

    and finally about your question, you can convert UserID and ClassID before your query, like this:

    var UID = int.Parse(UserID);
    var CID = int.Parse(ClassID);
    var record = context.enrollments.SingleOrDefault
        (row => row.userId == UID && row.classId == CID);
    
    0 讨论(0)
  • You can use AsEnumerable() as follows:

    var record = context.enrollments.AsEnumerable().FirstOrDefault(row => row.userId == Convert.ToInt32(UserID) && row.classId == Convert.ToInt32(ClassID));
    
    0 讨论(0)
  • 2020-11-28 00:16

    This works for me:

      var iId = int.Parse(TextBox1.Text);
     var item = db.Items.Where(p => p.ItemID == iId ).FirstOrDefault();
    

    To make that work you should convert your textbox value outside of the lambda expression.

    0 讨论(0)
提交回复
热议问题