Filtering on different levels

Posts   
 
    
philipp
User
Posts: 53
Joined: 15-Feb-2007
# Posted on: 15-Jul-2008 17:12:21   

Hi there

Lets say I have in-memory collections of two entity classes with an 1:n relation: An _EntityCollection<Customer>, _and each customer entity provides a property Orders of type EntityCollection<Order>.

What I want to do now is to create a view that only shows me some customers (e.g. all customers that start with "A") and of these customers, only some orders (e.g. all orders that were placed this year).

As far as I understand entity views, I can easily create a view that filters the customer collection, optionally even including constraints regarding related entities (the orders). However: Is there a way to get a result that also filters the _Orders _collections (automatically applies a filter to all _Order _collections of the filtered _Customer _entities?

myCustomerEntityCollection.CreateView(xxx);

Example: (get orders of 2008 of customers starting with "A")

Unfiltered data


Customer "Anne", Orders collection with 6 orders (2 this year) Customer "Axel", Orders collection with 4 orders (3 this year) Customer "Bart", Orders collection with 2 orders (1 this year)

Filtered data (both levels)


Anne, Orders property returns 2 orders Axel, Orders property returns 3 orders

Thanks for your advice Philipp

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 16-Jul-2008 07:19:24   

If you are using LLBLGen v2.x, you should do that this way: http://llblgen.com/TinyForum/Messages.aspx?ThreadID=7100&StartAtMessage=0&#39212

If you are using v2.6 and .NEt3.5+, you could use LINQ to Objects, so given an EntityCollection<CustomerEntity> named customers, you could do that this way:

var results = (from c in customers                      
                           where c.CustomerId.StartsWith("A") && c.Orders.Where(o => o.OrderDate.Value.Year >= 2008).Count() > 0
                           select c);
David Elizondo | LLBLGen Support Team
philipp
User
Posts: 53
Joined: 15-Feb-2007
# Posted on: 16-Jul-2008 09:44:22   

daelmo wrote:

If you are using LLBLGen v2.x, you should do that this way: http://llblgen.com/TinyForum/Messages.aspx?ThreadID=7100&StartAtMessage=0&#39212

If you are using v2.6 and .NEt3.5+, you could use LINQ to Objects, so given an EntityCollection<CustomerEntity> named customers, you could do that this way:

var results = (from c in customers                      
                           where c.CustomerId.StartsWith("A") && c.Orders.Where(o => o.OrderDate.Value.Year >= 2008).Count() > 0
                           select c);

Wouldn't the above statement filter my customers based on customer and order information, but still return all orders for the selected customers? So in order to get back to my example:

_ Expected result


Anne, Orders property returns 2 orders Axel, Orders property returns 3 orders

Returned result from above statement


Anne, Orders property returns 6 (all) orders Axel, Orders property returns 4 (all) orders_

Cheers, Philipp

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 16-Jul-2008 10:48:40   

Either you filter them manually (iterating on the customers), and copy the result graph into a new one.

Or better to filter in the database side, by fetching the customers and orders (using prefetchPaths, where you can set a filter to the prefetchPath).

philipp
User
Posts: 53
Joined: 15-Feb-2007
# Posted on: 16-Jul-2008 20:38:07   

Walaa wrote:

Either you filter them manually (iterating on the customers), and copy the result graph into a new one.

Or better to filter in the database side, by fetching the customers and orders (using prefetchPaths, where you can set a filter to the prefetchPath).

Getting data from the database is not an option - I already have the data in memory, and the entities might not even be stored in the DB yet (it's a synchronization tool).

Could you elaborate on "copying the result graph? I think I can't just copy my Order entites into another entity, or this will cause the order to be removed from its original parent entity (the customer), doesn't it?

Cheers, Philipp

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 17-Jul-2008 07:11:03   

philipp wrote:

daelmo wrote:

If you are using LLBLGen v2.x, you should do that this way: http://llblgen.com/TinyForum/Messages.aspx?ThreadID=7100&StartAtMessage=0&#39212

If you are using v2.6 and .NEt3.5+, you could use LINQ to Objects, so given an EntityCollection<CustomerEntity> named customers, you could do that this way:

var results = (from c in customers                      
                           where c.CustomerId.StartsWith("A") && c.Orders.Where(o => o.OrderDate.Value.Year >= 2008).Count() > 0
                           select c);

Aha... this should do the trick:

var results = (from c in customers
               where c.CustomerId.StartsWith("A") 
                          && c.Orders.Where(o => o.OrderDate.Value.Year >= 2000).Count() > 0
               select new { 
                   c.ContactName,
                   c.Country,
                   ... ,
                   Orders = c.Orders.Where(o => o.OrderDate.Value.Year >= 2000)
               });

philipp wrote:

Walaa wrote:

Either you filter them manually (iterating on the customers), and copy the result graph into a new one.

Or better to filter in the database side, by fetching the customers and orders (using prefetchPaths, where you can set a filter to the prefetchPath).

... Could you elaborate on "copying the result graph? I think I can't just copy my Order entites into another entity, or this will cause the order to be removed from its original parent entity (the customer), doesn't it?

I think that is FindMatches on you collection, then iterate on the collection using the indexes and then add the founded entities to a new collection (you can clone entities: create an entity and then clone the old entity's fields to the new entity via EntityFields2.Clone() ).

David Elizondo | LLBLGen Support Team