Print Page | Close Window

Query Single Entity

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2676
Printed Date: 27-Mar-2025 at 11:34pm


Topic: Query Single Entity
Posted By: Randar
Subject: Query Single Entity
Date 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.




Replies:
Posted By: sbelini
Date 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 http://drc.ideablade.com/xwiki/bin/view/Documentation/async-immediate-execution - DevForce Resource Center .
In the http://drc.ideablade.com/xwiki/bin/view/Documentation/query - DevForce Resource Center you will also find valuable information about querying.
 
Silvio.


Posted By: Randar
Date Posted: 16-May-2011 at 7:19am
That worked.  Thanks.



Print Page | Close Window