[V2 Adapter] Entitycollection: find single childentity

Posts   
 
    
BertS
User
Posts: 89
Joined: 28-Jul-2005
# Posted on: 22-Jul-2006 12:59:52   

Hi,

I have an entitycollection with SalesReports and prefetched the child-SalesReportLine's. Now I need to find in-memory the SalesReportLine where 'ExternalId=_id'. This query (executed against the database) returns always 0 or 1 records, because ExternalId is unique (but nullable, so it does not have an unique contraint)

Is it possible (i.e. with EntityView, but how to specfiy de predicate?), to do this in-memory in an other way than:

For Each report As SalesReportEntity In myCollection
  For Each line As SalesReportLineEntity In report.SalesReportLine
    If line.ExternalId = _id Then
      return line
    End If
  Next
Next

?

It is possible in the above way, but I don't really like it wink but it may be purism... smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 22-Jul-2006 16:12:26   

You can create a view, but you can also find the index directly.

Dim indices As List(Of Integer) = myCollection.FindMatches((SalesReportFields.ExternalId = _id))
If (indices.Count > 0) Then
    Return myCollection(indices(0))
Else
    Return Nothing
End If

Frans Bouma | Lead developer LLBLGen Pro
BertS
User
Posts: 89
Joined: 28-Jul-2005
# Posted on: 24-Jul-2006 13:43:25   

Hey, that's great. simple_smile Thanx! I'm not familiar with 'FindMatches', so I was not thinking that way.

BertS
User
Posts: 89
Joined: 28-Jul-2005
# Posted on: 26-Jul-2006 10:20:20   

Sorry, this is not a working solution. The problem is that I have a collection of SalesReports, with for each SalesReportEntity in that collection a child-collection with SalesReportLines. I have to find a SalesReportLine in the most efficiƫnt way. The field ExternalId is in the SalesReportLine. With your solution, I'm searching in the SalesReports, not in the Lines.

It is something like union-ing all the SalesReportLine-collections, and then perform a search.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 26-Jul-2006 14:53:40   

I think you should either use your simple logic, or combine the 2 approaches as follows:

For Each report As SalesReportEntity In myCollection
    Dim indices As List(Of Integer) = report.SalesReportLine.FindMatches((SalesReportLineFields.ExternalId = _id))
    If (indices.Count > 0) Then
        Return report.SalesReportLine(indices(0))
    End If
Next