Predicate example

Posts   
 
    
Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 18-Jan-2007 18:30:41   

Hi All,

I'm not sure how to formulate a predicate expression for this:

Tables: Employee(EIdent) <---> EmployeeDeptartment(EDIdent) <--> Department(DIdent)

The EmployeeDepartment is basically a junction table with some additional info.

I want a collection of all Employees for a specific Department (using DIdent).

I'm using Adapter, .net 2.0.

Thanks,

Fishy

psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 18-Jan-2007 19:28:10   

Fishy wrote:

I'm not sure how to formulate a predicate expression for this:

Tables: Employee(EIdent) <---> EmployeeDeptartment(EDIdent) <--> Department(DIdent)

The EmployeeDepartment is basically a junction table with some additional info.

I want a collection of all Employees for a specific Department (using DIdent).

I'm using Adapter, .net 2.0.

Hey Fishy,

From your description, it sounds like you just have to create a relationship (inner join hint) from Employee to EmployeeDepartment and then filter on EmployeeDepartment.DIdent.

What part are you struggling with?

Phil

Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 18-Jan-2007 19:40:59   

I thought there would be someway to go directly to Employee thru some type of FetchCollection via the EmployeeDepartment.

psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 18-Jan-2007 19:52:54   

Fishy wrote:

I thought there would be someway to go directly to Employee thru some type of FetchCollection via the EmployeeDepartment.

Assuming I understand you correctly, you could fetch the EmployeeDepartment filtered on DIdent, with a prefetch path to employee.

But then the collection you would have would be EmployeeDepartments, each with an employee attached to it. I don't think you can fetch a collection of Employees via the EmployeeDepartment entity, as the relationship from ed => e should be many to one.

HTH,

Phil

Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 18-Jan-2007 20:04:34   

I did find this code in the manual.

' VB.NET, .NET 2.0
Dim customer As New CustomerEntity("CHOPS")
Dim adapter As New DataAccessAdapter()
Dim orders As EntityCollection(Of OrderEntity)= customer.Orders
adapter.FetchEntityCollection(orders, customer.GetRelationInfoOrders())

Which, I think, is what I want to do.

Thanks.

psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 18-Jan-2007 20:46:04   

Fishy wrote:

I did find this code in the manual.

' VB.NET, .NET 2.0
Dim customer As New CustomerEntity("CHOPS")
Dim adapter As New DataAccessAdapter()
Dim orders As EntityCollection(Of OrderEntity)= customer.Orders
adapter.FetchEntityCollection(orders, customer.GetRelationInfoOrders())

Which, I think, is what I want to do.

Thanks.

I don't think this is possible in your case, since the related entities you want to fetch do not comprise a collection, but rather a single entity. Each EmployeeDepartmentEntity will have one and only one EmployeeEntity related to it, which (as far as I know) can't be fetched as a collection in the above manner.

If you reverse the above example, and think in terms of fetching a customer collection via the orders collection (which would not be possible), it would be a closer analogy to your own situation.

Could you post a snippet of code that you can't get working? It might make it easier to directly address the problem (plus it's possible I'm simply not understanding your questions).

Phil

psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 18-Jan-2007 23:05:54   

I don't have a VB project that will allow me to syntax check this code, but something close to the following should work:


public function GetEDCollection(ByVal departmentId as int) as EntityCollection (of EmployeeEntity)

    Dim ED As New EntityCollection (of EmployeeEntity)

    Dim bucket as new RelationPredicateBucket()
    bucket.Relations.Add(EmployeeEntity.Relations.EmployeeDepartmentUsingEmployeeId)
    bucket.PredicateExpression.Add(EmployeeDepartmentFields.DepartmentId == departmentId)

    Dim adapter as new DataAccessAdapter()
    adapter.FetchEntityCollection(ED, bucket)

    return ED

end function


Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 19-Jan-2007 16:40:14   

Thank you Phil.

Your code was perfect. simple_smile Just exactly what I needed.

Thanks again,

Fishy