相关文章推荐
文武双全的钥匙扣  ·  C#代码中用UL 0B ...·  4 月前    · 
帅气的青蛙  ·  KeyboardView 类 ...·  9 月前    · 
瘦瘦的泡面  ·  C++:Libcurl ...·  9 月前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Unable to cast object of type 'System.Data.Linq.DataQuery`1[System.Int32]' to type 'System.IConvertible'

Ask Question
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:

Unable to cast object of type 'System.Data.Linq.DataQuery`1[System.Int32]' to type 'System.IConvertible'

I hope no-one minds - I've fixed the indentation to show where the statements are more clearly. – Jon Skeet Apr 27, 2009 at 6:27

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()

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.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.