Unable to pass an object of type "System.Data.Linq.DataQuery`1 [System.Int32]" to enter "System.IConvertible"

I am trying to insert data into my database which has 2 tables

Products
(ProductID): 1
(IDNumber): 200900110
(ProductName): Pepsi

To order
(OrderID): 1 (Auto increase by 1)
(ProductID): 1
(Date): 1/1/2009

The code looks like this:

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 starting, I get this error:

Cannot start object of type 'System.Data.Linq.DataQuery`1 [System.Int32] to enter "System.IConvertible"

+7
c # linq linq-to-sql
source share
1 answer

Your request:

 from p in db.Product where p.IDNumber == 200900110 select p.ProductID 

does not return a single result, but a list of results. In your case, it will be a list containing one product identifier. You should change it to this:

 (from p in db.Product where p.IDNumber == 200900110 select p.ProductID).Single() 

If you run your code in the debugger and hover over the idNum variable, you will see that it is an instance of DataQuery .

+27
source share

All Articles