Sometimes I forget how truly powerful NHibernate is. I managed to
achieve the above using filters.

So this is what we want to do -

Give me all Payroll batches that contain items that are linked to
driver x
When I access the batch in some way, filter it's collections for this
driver, so total will show just the totals for the driver.

// enable filter
            session.EnableFilter("driver")
                .SetParameter("driverId", _driver.Id);

            var batchItems = DetachedCriteria.For<PayrollBatchItem>()
                .SetProjection(Projections.Property("PayrollBatch.Id"));

            var query = session.CreateCriteria<PayrollBatch>()
                .Add(Subqueries.Exists(batchItems))
                .List<PayrollBatch>();

This was almost too easy.

On Jun 30, 6:17 pm, Ben <[email protected]> wrote:
> Hi,
>
> In my domain model I have:
>
> PayrollBatch > (has many) PayrollBatchItem > (has one) Driver.
>
> So let's say I want to "get all payroll batches for driver x"
>
> I am doing this using DetachedCriteria:
>
>             var batchItems = DetachedCriteria.For<PayrollBatchItem>()
>                 .Add(Restrictions.Eq("Driver.Id", _driver.Id))
>                 .SetProjection(Projections.Property("PayrollBatch.Id"));
>
>             var query = session.CreateCriteria<PayrollBatch>()
>                 .Add(Subqueries.Exists(batchItems))
>                 .List<PayrollBatch>();
>
> This works great and any batches that contain payroll batch items
> associated with the driver are returned.
>
> My question:
>
> PayrollBatch has a property "Total" that calculates the total value of
> it's PayrollBatchItems.
>
> How can I change my criteria so that I return all payroll batches for
> driver x, with each batch only containing the driver's payroll items.
>
> This way when I show a list of the batches and their totals, they will
> display the total for the DRIVER's items.
>
> Thanks,
> Ben

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to