C# Linq基于SQL中存储的字符串返回单个整数值
我是Linq的新手 我只是想做一个常规的返回,这通常是用SQL完成的,似乎无法从浏览了几个小时的论坛中得到一个直接的答案 好的,我有一个表Customers,它有两列,CustomerID和CustomerName,它们属于customerDatabase 我已成功地使用以下方法(基本形式)从整数查询返回单个字符串: 好的,这很好,但是当我尝试返回CustomerID时,它返回一个异常:System.InvalidCastException:指定的强制转换无效 这是我的代码:C# Linq基于SQL中存储的字符串返回单个整数值,c#,.net,sql,linq,linq-to-sql,C#,.net,Sql,Linq,Linq To Sql,我是Linq的新手 我只是想做一个常规的返回,这通常是用SQL完成的,似乎无法从浏览了几个小时的论坛中得到一个直接的答案 好的,我有一个表Customers,它有两列,CustomerID和CustomerName,它们属于customerDatabase 我已成功地使用以下方法(基本形式)从整数查询返回单个字符串: 好的,这很好,但是当我尝试返回CustomerID时,它返回一个异常:System.InvalidCastException:指定的强制转换无效 这是我的代码: public in
public int getCustomerID(string custName)
{
CustomerDataBase cdb = new CustomerDataBase();
int custID = (
from c in cdb.Customers
where c.CustomerName == custName
select c.CustomerID)
.SingleOrDefault();
return custID;
}
我还尝试使用SQL术语将custName字符串设置为字符数组:
public int getCustomerID(string custName)
{
CustomerDataBase cdb = new CustomerDataBase();
int custID = (
from c in cdb.Customers
where c.CustomerName == "'"+custName+"'"
select c.CustomerID)
.SingleOrDefault();
return custID;
}
异常消失,但似乎只返回CustomerID的“0”,即使我有数百个条目
因此,在SQL中,我只使用以下内容:
Select CustomerID
from Customers
where CustomerName="'"+custName+"'"
任何帮助都将不胜感激!谢谢
//编辑代码以使其工作(以下是Jon Skeet的回答):-
好的,这是有效的:
[表(Name=“TableCustomers”)]
公共类客户
{
CustomerDatabase cdb = new CustomerDatabase();
long custID = (from c in cdb.Customers
where c.CustomerName == custName
select c.CustomerID).SingleOrDefault();
return custID;
}
}
}
//用于在SQL数据库中容纳BigInt
公共长getCustomerID(字符串custName)
{
CustomerDatabase cdb = new CustomerDatabase();
long custID = (from c in cdb.Customers
where c.CustomerName == custName
select c.CustomerID).SingleOrDefault();
return custID;
}
}
您的最后一种方法肯定是错误的-您不需要手动“引用”参数;基本上您是在用引号搜索一个值,它从SingleOrDefault
返回0
,因为它没有找到条目。(这就是SingleOrDefault
所做的-它返回类型的默认值。)(int
,此处)如果没有结果。)
我怀疑您的CustomerID
列是可空的,或者类似的,但是如果不了解您的模式,就很难判断。您应该检查您的LINQ to SQL模型是否与您的数据库模式匹配
您也应该考虑如果有多个同名客户,您将要发生什么——当前这将引发异常;这就是您想要的吗?
编辑:好的,现在你终于告诉我们模式有什么了,很清楚。
BigInt
是一个64位整数,但是你在LINQ模型中使用了一个32位属性。只需在LINQ模型中将CustomerID
从int
更改为long
,将方法更改为返回long
,就可以了很好。最后一个查询将返回零条记录,OrDefault()部分将返回int 0。这就是为什么您不会得到强制转换异常。代码中CustomerId的类型是什么?[Column(IsPrimaryKey=true,IsDbGenerated=true)]public int CustomerId{get;set;}客户ID在SQL数据库以及我使用的主键格式:[Table(Name=“TableCustomers”)]中不可为空且为BigInt。[Column(IsPrimaryKey=true,IsDbGenerated=true)]public int CustomerID{get;set;}[Column]public string CustomerName{get;set;}代码在注释中无法正常工作。您可以编辑问题以添加更多信息。CustomerID不可为null,并且是主键。在本例中,我使用的是唯一的客户名称(我有处理此冲突的方法,但尝试在此简化,以查看如何尝试从字符串返回ID).我将再次检查模型。尽管要检查是否存在任何冲突..谢谢因此CustomerName不是Nullable@amundell:那么(数据库中)CustomerID的具体类型是什么?InvalidCastException
中是否有更多信息?好的,这是在ASP.Net 3.5 Web应用程序中:=以下错误[InvalidCastException:指定的强制转换无效。]System.Data.Linq.SqlClient.SqlProvider.Execute(表达式查询、QueryInfo查询信息、IOObjectReaderFactory工厂、对象[]parentArgs、对象[]userArgs、ICompiledSubQuery[]子查询、对象lastResult)+1143 System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(表达式查询、查询信息[]queryInfos、IObjectReaderFactory、Object[]userArguments、ICompiledSubQuery[]subQueries)+115这样就没有消息了,或者内部异常了?再说一次,数据库中的CustomerID是什么类型的?
CustomerDatabase cdb = new CustomerDatabase();
long custID = (from c in cdb.Customers
where c.CustomerName == custName
select c.CustomerID).SingleOrDefault();
return custID;
}
}