Understanding filtering of EntityViews

Posts   
 
    
Scooter
User
Posts: 2
Joined: 28-Jul-2006
# Posted on: 28-Jul-2006 21:48:44   

I am really trying to get my head around the filtering of entityviews. We just migrated to LLbL 2.0 and prior to that our code looked like this:

Dim CurrentRoleEntity As EntityClasses.RolesEntity

grdMain.DataSource = CurrentRoleEntity.MenuRoles

Well, I now have a need to apply a filter to this, and in all the reading both in LLBL forums and LLBL documentation, I gather that EntityViews are the way to go. However, I am struggling with how to apply that to the code above.

Any insight is greatly appreciated.

Thanks...

Scott

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 29-Jul-2006 04:04:54   

Give something like this a try. The view operates on the collection you already have in memory.

Dim CurrentRoleEntity As EntityClasses.RolesEntity

Dim menuRolesView As New EntityView(Of MenuRolesEntity)(CurrentRoleEntity.MenuRoles)

Dim filter As IPredicate = (MenuRoleFields.Role = "Admin")
menuRolesView.Filter = filter
gridMain.DataSource = menuRolesView

Let us know if you have any issues with this example.

Scooter
User
Posts: 2
Joined: 28-Jul-2006
# Posted on: 31-Jul-2006 17:26:35   

Thanks bclubb. Greatly appreciated. That worked great and certainly cleared things up a bit.

One last question. I have been researching this morning trying to find a way to join that in-memory entity to another entity (via a relation) and then apply a filter to the second entity using the code you sent (or something similar).

Did I explain that ok?

Thnaks again...

Scott

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 31-Jul-2006 18:43:43   

You can't filter an entityview on a related entity. If you want to do that, you should map a field mapper onto a related field (e.g. Order.CompanyName is mapped onto Order.Customer.CompanyName) and filter on that, using EntityProperty(). See for usage of Entityproperty the entityview documentation.

Frans Bouma | Lead developer LLBLGen Pro
alexdresko
User
Posts: 336
Joined: 08-Jun-2004
# Posted on: 10-Oct-2006 20:39:20   

Otis wrote:

You can't filter an entityview on a related entity. If you want to do that, you should map a field mapper onto a related field (e.g. Order.CompanyName is mapped onto Order.Customer.CompanyName) and filter on that, using EntityProperty(). See for usage of Entityproperty the entityview documentation.

Is this planned by any chance? If EntityViews supported the same filtering capabilities as standard collections, this would go a long way towards supporting unit testing..

For example, I want to write unit tests that verify my filters are working correctly but without actually having to go to the database. EntityViews would be perfect for that!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 11-Oct-2006 08:37:04   

alexdresko wrote:

Otis wrote:

You can't filter an entityview on a related entity. If you want to do that, you should map a field mapper onto a related field (e.g. Order.CompanyName is mapped onto Order.Customer.CompanyName) and filter on that, using EntityProperty(). See for usage of Entityproperty the entityview documentation.

Is this planned by any chance? If EntityViews supported the same filtering capabilities as standard collections, this would go a long way towards supporting unit testing..

It is planned, though I've no idea at this time how it will pan out, as I can see it will be rather tough to implement in memory, but nevertheless, I also want the feature so it's on the list simple_smile

Frans Bouma | Lead developer LLBLGen Pro
alexdresko
User
Posts: 336
Joined: 08-Jun-2004
# Posted on: 11-Oct-2006 15:08:25   

Otis wrote:

alexdresko wrote:

Otis wrote:

You can't filter an entityview on a related entity. If you want to do that, you should map a field mapper onto a related field (e.g. Order.CompanyName is mapped onto Order.Customer.CompanyName) and filter on that, using EntityProperty(). See for usage of Entityproperty the entityview documentation.

Is this planned by any chance? If EntityViews supported the same filtering capabilities as standard collections, this would go a long way towards supporting unit testing..

It is planned, though I've no idea at this time how it will pan out, as I can see it will be rather tough to implement in memory, but nevertheless, I also want the feature so it's on the list simple_smile

I was thinking about this some last night and I don't think it can produce guaranteed results.. The current filtering mechanism is dependent on whatever database the generated code is bound to. So if I create a filter before I fetch a collection, I'm really passing a query to SQL and telling it to return the results I want. SQL handles that logic. Whereas your in-memory filtering will be based on whatever algorythm you decide is best. Therefore, the results might not always be the same.

How likely do you think that would be though?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 12-Oct-2006 11:43:42   

I don't exactly understand what you mean by 'it can give different results'.

At the moment you can filter a view based on the data in the entities in the collection the view is a view of.

This should be extended to a filter mechanism which let you filter a view based on data related to that data in the entities in the collection the view is a view of, and I think that's doable, and should give the results you expect, as it works with in-memory data ONLY. So if you fetch a hierarchy, you should be able to get a view of a collection in that hierarchy based on a filter which filters on related data. For example: you fetch customers and orders. It should be possible to filter the customer view based on a filter on orders.

It's perhaps a hard thing to program, I'm not sure, I haven't looked into the gory details yet. The main thing is that it should be unwinded from leaf to root, using simple set logic: first filter the orders, then see if that set offers any results, yes then mark that predicate as true in the whole equation of predicates. If the whole set of predicates resolves to true for that customer, the customer is accepted.

Frans Bouma | Lead developer LLBLGen Pro
Jazz
User
Posts: 63
Joined: 12-Aug-2005
# Posted on: 09-Sep-2008 20:54:47   

Hi there,

I hate to bring that topic up again but is it still not possible to filter on a related entity? Fear I cannot use the EntityProperty as I got the following structure.

Order 1:n Prices and I want to filter on prices. I dont have the option to filter it from the database as I want to use both filtered and unfiltered collection on a disconnected client.

Regards, André

arschr
User
Posts: 893
Joined: 14-Dec-2003
# Posted on: 09-Sep-2008 22:08:55   

I believe entityviews can be filtered on values of fields in related entities, at least in 2.5ish versions.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 10-Sep-2008 08:39:04   

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:

var results = (from o in fetchedOrders
                         where o.Prices.Where(p => p. Discontinued == false).Count() > 0
                         select o);

List<OrderEntity> filteredInMemoryOrders = q.ToList();
David Elizondo | LLBLGen Support Team
Jazz
User
Posts: 63
Joined: 12-Aug-2005
# Posted on: 10-Sep-2008 16:48:49   

Thanks for your reply. Today I migrated from V2.5 to V2.6, but I'm not planning to use .NET 3.5 anytime soon. So with V2.6 and .Net 2.0 my problem won't be solved using the EntityView?

Regards, André

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 12-Sep-2008 20:24:57   

Hi André. In that case, this is the way I would do that:

In a partial class, write a property that return the number on prices accordingly to some in-memory filter.

namespace Northwind.Model.EntityClasses
{
    public partial class OrderEntity
    {
        public int DiscountedPricesCount
        {
            get
            {
                return this.Prices.FindMatches(PriceFields.Discountinued == false).Count;               
            }
        }
    }
}

Then you could use that property to create your EntityView:

EntityView2<OrderEntity> filteredInMemoryOrders = new EntityView2<OrderEntity>(fetchedOrders, new EntityProperty("DiscountedPricesCount") > 0);
David Elizondo | LLBLGen Support Team
Jazz
User
Posts: 63
Joined: 12-Aug-2005
# Posted on: 15-Sep-2008 17:52:48   

Thank you David, that did the job!

Regards, André