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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with ObjectQuery 1

Status
Not open for further replies.

blairacuda

Technical User
Sep 3, 2009
51
US
i am just getting into the Entity Framework stuff of 4.0 and i'm running into a little issue that is driving me crazy.

Here's my code:

var partTypeGUIDQuery = from p in ecEntities.Parts where p.PartID == pGUID select p.PartTypeID;
foreach (Guid g in ((ObjectQuery)partTypeGUIDQuery).Execute(MergeOption.AppendOnly))
{
partTypeGUID= g;
}

basically my question is that if i know i'm only going to get one result from the ObjectQuery what should i do instead of the foreach. everything i've tried hasn't worked and its annoying me to foreach on each of my object queries.

thanks in advance

CBlair
Crystal, InstallShield, branching out in other programming realms.
 
there should be a method like Get/Load/Retrieve something like that where you can get a single entity by id. Not sure what it is in EF, but in Nhibernate you have 2 options
Code:
var entity = session.Get<Entity>(id);
var entity = session.Load<Entity>(id);
something similar should exist in EF.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Assuming that PartTypeID is a GUID, this should load partTypeGUIDQuery with a single GUID. Keep in mind that if there are multiple results, you will only have access to the first one.

Code:
var partTypeGUIDQuery = (from p in ecEntities.Parts 
                                     where p.PartID == pGUID 
                                     select p.PartTypeID).FirstOrDefault();

--Mark
 
gryph-

thats perfect. thanks for your help. there are a handful of queries where i know only one result will be returned and this works brilliantly. thanks.

-chris

CBlair
Crystal, InstallShield, branching out in other programming realms.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top