find child objects in a collection

find child objects in a collection

Old forum URL: forums.lhotka.net/forums/t/6712.aspx


DutchDiesel posted on Wednesday, April 01, 2009

I'm trying to find a child object in a collection by a certain property of the child object.

Do I have to create my own function to implement this functionality or does CSLA offer me something to do this.


sergeyb replied on Wednesday, April 01, 2009

You can just use LINQ.

 

Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: DutchDiesel [mailto:cslanet@lhotka.net]
Sent: Wednesday, April 01, 2009 8:10 AM
To: Sergey Barskiy
Subject: [CSLA .NET] find child objects in a collection

 

I'm trying to find a child object in a collection by a certain property of the child object.

Do I have to create my own function to implement this functionality or does CSLA offer me something to do this.




reagan123 replied on Wednesday, April 01, 2009

I'm not sure if this is a good approach or not, but i've done something like this.  This assumes that there is only one child object that contains that property. 

*** I think the following is right... I typed it from memory.

 Public Function GetItem(ByVal someValue As String) As Child

            For Each c As Child In Me
                If c.SomeValue = someValue Then
                    Return c
                End If
            Next
            Return Nothing

 End Function

DutchDiesel replied on Wednesday, April 01, 2009

I implemented a for loop myself,
but I was wondering if CSLA has a built in implementation for such a functionality?

Indeed this is only possible when only one child contains the property, wich in my case is true,
because the property which I use to find the child object is the PrimaryKey in the database.

JoeFallon1 replied on Thursday, April 02, 2009

You can easily add such a method to your intermediate base class that sits between CSLA and your BOs. (You *do* have such a class for each type of BO, right?)

Something like this - which relies on the optional GetIdValue method (which used to be mandatory).

Public Overloads ReadOnly Property ItemById(ByVal key As Object) As C
Get
Dim obj As IEditableBusinessObject
For Each obj In Me

If obj.GetIdValue.Equals(key) Then
Return DirectCast(obj, C)
End If

Next
Return Nothing
End Get
End Property

Joe

Copyright (c) Marimer LLC