我在下面的代码中遇到错误,如何解决此错误 [英] I am getting Error in following code,how to solve this error

查看:87
本文介绍了我在下面的代码中遇到错误,如何解决此错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误是:LINQ to Entities无法识别方法'Int32 ToInt32(System.String)'方法,并且此方法无法转换为商店表达式。



  string  adID = txtSearchRegNo.Text; 
// int adID = Convert.ToInt32(txtSearchRegNo.Text);

var list =( from p in db.AdmissionDetails join s in db.StudentDetails on p.AdmissionId equals s.AdmissionId
其中 p.AdmissionId == adID
选择 new
{
RegNo = p.AdmissionId,
Student_Name = p.StudentFName + + p.StudentMName + + p。 StudentLName,
Mother_Name = p.MotherFanme,
Cast = p.Cast,
s.Nationa lity,
Place_Of_Birth = p.PlaceofBirth,
Mother_Tongue = s.MotherTongue,
Date_Of_Birth = p.BirthDate,
})。ToList();

解决方案

转换不应该是从用户输入字符串中获取整数值的首选方式。



用户输入应始终有效。最好的方法是使用 TryParse 方法。这样:

  int  adID; 
if (!int.TryParse(txtSearchRegNo.Text, out adID){
throw new ArgumentException( 输入的文本不是有效的整数表示。);
}
// < span class =code-comment>在这里你可以用adID变量做任何你想做的事





希望这会有所帮助。


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

string  adID = txtSearchRegNo.Text;
//  int adID =Convert.ToInt32(txtSearchRegNo.Text);

var list = (from p in db.AdmissionDetails join s in db.StudentDetails on p.AdmissionId equals s.AdmissionId
            where p.AdmissionId==adID
            select new
            {
               RegNo = p.AdmissionId ,
               Student_Name = p.StudentFName +" "+ p.StudentMName +" "+ p.StudentLName,
               Mother_Name = p.MotherFanme,
               Cast = p.Cast,
               s.Nationality,
               Place_Of_Birth= p.PlaceofBirth,
               Mother_Tongue = s.MotherTongue,
               Date_Of_Birth = p.BirthDate,
             }).ToList();

解决方案

Convert should not be your preferred way of getting an integer value from a user-input string.

User inputs should ba validated at all times. THe best way for that is to use the TryParse method. This way:

int adID;
if (!int.TryParse(txtSearchRegNo.Text, out adID) {
   throw new ArgumentException("Entered text is not a valid integer representation.");
}
// Here you can make whatever you want with adID variable



Hope this helps.


这篇关于我在下面的代码中遇到错误,如何解决此错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆