无法投类型的对象System.Data.Linq.DataQuery`1 [System.Int32]'键入'System.IConvertible“ [英] Unable to cast object of type 'System.Data.Linq.DataQuery`1[System.Int32]' to type 'System.IConvertible'
问题描述
有人可以帮我找出这个问题的解决方案?
Can somebody please help me to find out the solution with this problem?
我试图将数据插入到我的数据库有2个表
I'm trying to insert the data into my database which has 2 tables
产品
(产品ID):1结果
(IDNumber中):200900110结果
(产品名称):百事可乐< BR>
结果
阶结果
(订单id):1(自动递增1)结果
(产品ID):1结果
(日期):2009/1/1结果
Products
(ProductID): 1
(IDNumber) : 200900110
(ProductName) : Pepsi
Order
(OrderID): 1 (Auto Increment by 1)
(ProductID):1
(Date): 1/1/2009
中的代码是这样的:
var db = new ProductOrder();
var idNum = from p in db.Product
where p.IDNumber == 200900110
select p.ProductID;
var order = new Order();
order.productID = Convert.ToInt32(idNum);
order.Date = DateTime.Now;
db.Order.InsertOnSubmit(nTime);
db.SubmitChanges();
在我运行它给我的错误是这样的:
After I run it gives me the error like this :
无法转换类型
的对象System.Data.Linq.DataQuery`1 [System.Int32]'
键入System.IConvertible
Unable to cast object of type 'System.Data.Linq.DataQuery`1[System.Int32]' to type 'System.IConvertible'
可能有人请检查我这个代码?感谢在进步
May someone please check this code for me? Thanks in advances.
推荐答案
您查询:
from p in db.Product
where p.IDNumber == 200900110
select p.ProductID
不返回一个结果,但结果的列表。在你的情况,这将是一个包含单个产品ID列表。你应该把它修改这样:
does not return a single result but a list of results. In your case this will be a list containing a single product id. You should modify it to this:
(from p in db.Product
where p.IDNumber == 200900110
select p.ProductID).Single()
如果您在调试器中运行你的代码,你将鼠标悬停在 IDNUM
变量,你会看到它是一个 DataQuery
实例。
If you run your code in the debugger and you hover your mouse over the idNum
variable, you'll see that it is a DataQuery
instance.
这篇关于无法投类型的对象System.Data.Linq.DataQuery`1 [System.Int32]'键入'System.IConvertible“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!