New Posts New Posts RSS Feed: Query Single Entity
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Query Single Entity

 Post Reply Post Reply
Author
Randar View Drop Down
Newbie
Newbie
Avatar

Joined: 13-May-2011
Location: Toronto
Posts: 23
Post Options Post Options   Quote Randar Quote  Post ReplyReply Direct Link To This Post Topic: Query Single Entity
    Posted: 13-May-2011 at 10:03am
I am completely new to DevForce, so bear with me.

Simple question.  What is the best practice for getting a single entity, based on a primary key, in this case ID.  Everything in DF seems to get back a collection and then you have to get the first item in the collection.  Is there a shortcut for getting a single entity?  Here is what I am using right now:

Dim emg As EntityManager_GL = New EntityManager_GL()
Dim atQuery = From accountType In emg.GLAccountTypes
              Where accountType.ID = ID
              Select accountType

Dim itemList = atQuery.ToList() ' TODO: Is there a better way?

If (Not itemList Is Nothing And itemList.Count > 0) Then
    Dim item = itemList(0) ' Get the first item in the list

    ' Map it to the current record
    With item
        m_ID = .ID
        m_CompanyID = .CompanyID
        m_Description = .Description
    End With
End If

p.s. I haven' tested this code because I am having issues with my web service.  But that just seems to be my environment.

Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 13-May-2011 at 10:52am
Hi Randar,
 
Try .First() or .FirstOrDefault() instead of .ToList().
 
Note that if you are working asynchronously (i.e. Silverlight) you will need to use .AsScalarAsync()
 
var queryOp = _entityManager.GLAccountTypes
.Where(gla => gla.ID == ID)
.AsScalarAsync()
.First();
 
Also have in mind that in this case you will need to wait for the callback to be able to retrieve the result (i.e. queryOp does not hold the result)
 
You will find more information about executing scalar queries async in the DevForce Resource Center.
In the DevForce Resource Center you will also find valuable information about querying.
 
Silvio.


Edited by sbelini - 13-May-2011 at 10:55am
Back to Top
Randar View Drop Down
Newbie
Newbie
Avatar

Joined: 13-May-2011
Location: Toronto
Posts: 23
Post Options Post Options   Quote Randar Quote  Post ReplyReply Direct Link To This Post Posted: 16-May-2011 at 7:19am
That worked.  Thanks.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down