我这里得到的错误是我的代码
var query1 = (from cust1 in context2.Clients
where cust1.ID == int.Parse(textBox1.Text)
select cust1).FirstOrDefault();`
即使我的ID是Int而不是字符串
答案 0 :(得分:1)
在查询之外调用Parse
方法:
var id=int.Parse(textBox1.Text);
var result=context2.Clients.FirstOrDefault(cust=> cust.ID ==id );
答案 1 :(得分:0)
我想最简单的解决方案是在查询之前转换为int
:
var id = int.Parse(textBox1.Text);
var query1 = (from cust1 in context2.Clients
where cust1.ID == id
select cust1).FirstOrDefault();
或者,为了错误处理:
var id = 0;
if (!int.TryParse(textBox1.Text, out id)) {
// raise an error condition
}
var query1 = (from cust1 in context2.Clients
where cust1.ID == id
select cust1).FirstOrDefault();
答案 2 :(得分:0)
首先必须将int.Parse设置为变量,如下所示:
var intValue = int.Parse(textBox1.Text);
var query1 = (from cust1 in context2.Clients
where cust1.ID == intValue
select cust1).FirstOrDefault();