Using PredicateExpressions with two foreign keys

Posts   
 
    
rumplyminz
User
Posts: 4
Joined: 04-May-2007
# Posted on: 04-May-2007 20:14:05   

Where x is the current Enumeration:

AttributeClassEntity ace = (AttributeClassEntity)x.Current;
IPredicateExpression filter = new PredicateExpression();
                filter.Add(AttributeListingsFields.ListingId == l.Id);
                filter.AddWithAnd(AttributeListingsFields.AttributeClassId == ace.Id);

RelationPredicateBucket bucket=new RelationPredicateBucket();
                bucket.PredicateExpression.Add(filter);

EntityCollection attrListings = new EntityCollection(new AttributeListingsEntityFactory());
                adapter.FetchEntityCollection(attrListings,bucket);

IEnumerator y = attrListings.GetEnumerator();
while (y.MoveNext())
{
       AttributeListingsEntity listing = (AttributeListingsEntity)y.Current;
       System.Diagnostics.Debug.WriteLine(listing.Description);
}

Using LLBL 2.0.50727 with C# and Adapter.

I have a table called AttributeListings, which has rows dependent upon a combination of ListingId and AttributeClassId. I want to get the collection of all of these rows. The above code works. But it seems like waaaaaay too much code, that there should be a 'more better cleaner easier' solution.

Thoughts?

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 04-May-2007 20:41:49   

do you mean something like this: each AttributeClassEntity in the AttributeClassEntity Collection contains a collection of AttributeListingEntities?

you may be able to use a custom relation between the 2 entities and a prefetch path. this would return a populated graph.

Knowing where l.Id comes from would help determine the best solution.

rumplyminz
User
Posts: 4
Joined: 04-May-2007
# Posted on: 04-May-2007 22:17:50   

Tables Listing AttributeListing AttributeClass Attribute

-l is a listing, so l.id is the primary key of the Listing Entity which I already have.

-Attribute is a table holding generic attributes which may apply to different classes.

-AttributeClass is a table storing the IDs of the Attributes which apply to a particular Class ID.

-AttributeListing is a table storing both the ID of the AttributeClass and the ID of the Listing (this combination is unique) which then has some extra fields to store information which the user may supply for this particular listing.

So, whenever I pull up a Listing, I either a) create a new one, or b) load up an existing one. It it already exists, I need to use this ListingID to pull up all Attributes through the AttributeClass table, linked from the AttributeListing table.

Anyway, just seemed like I was supplying a lot of code for doing something relatively simple. If this is just 'the way it is', then I can live with that - but I thought it would behoove me to reach out and ask. I am willing to go back to the designer and add stuff, or rewrite the code - just looking to maximize my investment in LLBL Gen Pro.

Thanks! Josh

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 04-May-2007 22:40:24   
public IEntityCollection2 GetListingsWithAtrributes([args for filtering])
{
    IEntityCollection2 listOfListings = new EntityCollection(new ListingEntityFactory());

    IPrefetchPath2 prefetch = New PrefetchPath2((int)EntityType.ListingEntity);
    prefetch.Add(ListingEntity.PrefetchPathAttributeListings).SubPath.Add(AttributeListingEntity.PrefetchPathAttributeClasss).SubPath.Add(AttributeClassEntity.PrefetchPathAttributes);

    //create filter based on arguments
    IRelationPredicateBucket bucket = new RelationPredicateBucket();
    bucket.PredicateExpression.Add(...);
    bucket.PredicateExpression.Add(...);
    bucket.PredicateExpression.Add(...);
    
    //query database
    using(IDataAccessAdapter adapter = new DataAccessAdatper())
    {
           adapter.FetchCollection(listOfListings, bucket, prefetch);
    }

    return listOfListings;
}

I took this straight from the help files and changed the entity names. I also used interfaces instead of classes. the biggest difference was your prefetch graph is deeper than the example in the help docs.

rumplyminz
User
Posts: 4
Joined: 04-May-2007
# Posted on: 04-May-2007 23:03:14   

Thanks a bunch. I will try it out, and let you know.

How's Harrisburg? Its been a year since my last visit to Gilligans, I am going to have to make the trip out again -

Josh

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 07-May-2007 14:17:25   

HBG isn't much different than the last time you visited. Probably a new restaurant or two downtown on 2nd st. They are also building a new structure at Herr & Cameron. I think it's a new parking garage.

rumplyminz
User
Posts: 4
Joined: 04-May-2007
# Posted on: 07-May-2007 15:45:39   

Using Prefetch Paths worked out for me. Thanks for the help!