Otis wrote:
No. If you want all records from HREmployee and with those fetch all salarypayment records, you should start with hremployee and create a path to fetch salarypayments. (this is a left join-esk fetch anyway).
The prefetch path then first fetches teh HREmployee entities, and after that for each HREmployee found, it fetches the available HRSalaryPayments. If no salary payment entity is available, none is loaded (which is effectively a left join).
How to amend this code to do what you are suggesting ??
Dim prefetch As IPrefetchPath2 = HREmployee.CreatePrefetchPath
prefetch.Add(HREmployee.PrefetchPathHRSalaryPaymentCollection)
Dim relationFilter As IPredicateExpression = New PredicateExpression
With relationFilter
.Add( _
PredicateFactory.CompareValue(HRSalaryPaymentFieldIndex.StrPeriod, _
ComparisonOperator.Equal, _
Common.DateToSalaryPeriod(Today.Date)))
.AddWithAnd( _
PredicateFactory.CompareValue(HRSalaryPaymentFieldIndex.BlnIsPosted, _
ComparisonOperator.Equal, _
True))
End With
Dim filter As IRelationPredicateBucket = New RelationPredicateBucket
filter.Relations.Add( _
HREmployee.Relations.HRSalaryPaymentEntityUsingStrEmployeeId, JoinHint.Left).CustomFilter = relationFilter
Dim employees As IEntityCollection2 = HREmployee.FetchList(filter, prefetch, adapter)
Edit:
Ok.. if I uderstood ur instructions, I amended the code as follows:
Dim prefetch As IPrefetchPath2 = HREmployee.CreatePrefetchPath
prefetch.Add(HREmployee.PrefetchPathHRSalaryPaymentCollection, -1, relationFilter)
Dim employees As IEntityCollection2 = HREmployee.FetchList(filter, prefetch, adapter)
which generated the following query for HRSalaryPayment
exec sp_executesql N'SELECT [Senopsis].[dbo].[HRSalaryPayment].[cntId] AS [CntId], [Senopsis].[dbo].[HRSalaryPayment].[strPeriod] AS [StrPeriod], [Senopsis].[dbo].[HRSalaryPayment].[strEmployeeId] AS [StrEmployeeId],
...
FROM [Senopsis].[dbo].[HRSalaryPayment]
WHERE ( [Senopsis].[dbo].[HRSalaryPayment].[strEmployeeId] IN (@StrEmployeeId1, @StrEmployeeId2, @StrEmployeeId3)
AND ( ( [Senopsis].[dbo].[HRSalaryPayment].[strPeriod] = @StrPeriod4 AND [Senopsis].[dbo].[HRSalaryPayment].[blnIsPosted] = @BlnIsPosted5)))',
N'@StrEmployeeId1 nvarchar(40),@StrEmployeeId2 nvarchar(40),@StrEmployeeId3 nvarchar(40),@StrPeriod4 nvarchar(40),@BlnIsPosted5 bit',
@StrEmployeeId1 = N'EMP-1001', @StrEmployeeId2 = N'EMP-1002', @StrEmployeeId3 = N'EMP-1003', @StrPeriod4 = N'January-2006', @BlnIsPosted5 = 1
This is the result I was looking for...
NOTE:
just to be sure, for the maxEntities parameter in prefetch.Add, I use (-1) to indicate I want all entities...