Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert Object.ObjectQuery<int> to int 2

Status
Not open for further replies.

nevas92

IS-IT--Management
Mar 4, 2009
6
0
0
US
I have the following code:
<code>
using(WPEntities db = new WPEntities())
{

int quoteID =from f in db.vw_FileGeneral
where f.ProductionFileID == prodID
select f.FileID;

Convert.ToInt16(quoteID);
</code>

And I am getting exceptions due to ObjectQuery<int> not able to convert to int. The query is returning just one object, but of course it returns a list (of one item in this case). How do I get that one item's value into quoteID?

Thanks in advance!
 
maybe something like . . .

Code:
ObjectQuery<int> quoteIDList = from f in db.vw_FileGeneral
              where f.ProductionFileID == prodID
              select f.FileID;

int quoteID = quoteIDList[0];
Convert.ToInt16(quoteID)

Not tested?

Age is a consequence of experience
 
Select() returns a collection, even if the collection only has a single item. to get a single item you would need to use First, Last, Single, ElementAt, Aggregate, etc. in this case
Code:
return db.vw_FileGeneral
   .Where(f => f.ProductionFileID == prodID)
   .Select(f => f.FileId)
   .FirstOrDefault();

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Thanks for the help guys, both options worked great.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top