Get EntityType for an EntityCollection

Posts   
 
    
softwarea
User
Posts: 57
Joined: 05-Mar-2007
# Posted on: 03-Dec-2007 18:26:07   

Hi experts,

I'm trying to write a rather abstract lookup method with the following signature INPUT: - an EntityCollection and - a PredicateExpression OUTPUT: - PK of the found entity

So, the method head would like this:

private long LookUpEntityIDByFilterExpression(IEntityCollection searchTable, PredicateExpression searchExpression)

Unfortunately I do not know how to compute the EntityType to a given EntityCollection, so right now my function looks like this:

private long LookUpEntityIDByFilterExpression(IEntityCollection searchTable, IEntity searchEntity, PredicateExpression searchExpression)
        {
            long result = 0;

            try
            {
                searchTable.GetMulti(searchExpression);
                if (searchTable.Count > 0)
                {
                    foreach (IEntityField fld in searchEntity.Fields)
                    {
                        if (fld.IsPrimaryKey)
                            result = Convert.ToInt64(searchTable[0].Fields[fld.Name].CurrentValue);
                    }
                }
            }
            catch (Exception e)
            {
            }
            //Return result
            return result;
        }

Does anybody know how to do this, so I can make my function calls a little bit more efficient/shorter?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 04-Dec-2007 04:34:31   

So far the method is good. Even if you know the type of collection, you have to iterate the field collection to know the PK field.

Your method assumes that

  • the search criteria will returns only one result
  • all your entities have simple PK (one field)
  • all your entities have numeric PK

If that is true, the method does its work fine wink

David Elizondo | LLBLGen Support Team
softwarea
User
Posts: 57
Joined: 05-Mar-2007
# Posted on: 04-Dec-2007 07:16:30   

Hi daelmo,

thank you for your review and yes, you are right about all the assumptions you mentioned.

But sorry, I should have been a little bit more precise about my problem. In fact I know that my method is working fine. I was just wondering if it is neccessary to pass 3 parameters to the method instead of two.

So, right now I pass (1) an EntityCollection + (2) an Entity + (3) a PredicateExpression.

As I have to do lots of such calls it would make my work mich easier if I could conclude from a given EntityCollection to the corresponding Entity type (or vice versa) so that my method head would just need (1)+(3) or alternatively (2)+(3).

So, I guess my real question is: given just an EntityCollection, can I somehow compute the corresponding Entity type?

Would be great if you have any ideas on that!!! Thanks! Ingmar

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 04-Dec-2007 07:51:19   

Hi Ingmar,

Sorry, I didn't understand your concern. I think this method is shorter and do the same:

private long LookUpEntityIDByFilterExpression(IEntityCollection searchTable, IPredicateExpression searchExpression)
{
    long result = 0;
    
    searchTable.GetMulti(searchExpression);
    
    if (searchTable.Count > 0)
    {
        result = Convert.ToInt64( searchTable[0].PrimaryKeyFields[0].CurrentValue);
    }

    return result;
}
David Elizondo | LLBLGen Support Team
softwarea
User
Posts: 57
Joined: 05-Mar-2007
# Posted on: 04-Dec-2007 07:54:04   

Ahhhhhh, there is a PrimaryKeyFields[] property...;-) That looks great and is exactly what I was looking for. Thanks so much, David!!!!