Take(1) - Best Practise?

Posts   
 
    
ianvink
User
Posts: 394
Joined: 15-Dec-2006
# Posted on: 11-Jul-2008 19:56:57   

I have this code block which returns 1 entity using a Take(1)

Is there a more elegant way to get "Top 1" like behaviour?


public static IEntity2 GetConstructionEntity(string Scheme, string Class)
        {
            var items = (ILLBLGenProQuery) (from x in meta.PropertyOccupancy
                                            where x.Class == Class &&
                                                  x.Scheme == Scheme
                                            select x).Take(1);
        
            EntityCollection<PropertyOccupancyEntity> collection = items.Execute<EntityCollection<PropertyOccupancyEntity>>();

            if (collection.Count == 1)
                return collection[0];
            else
                return null;
        }

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 12-Jul-2008 05:48:09   

What about "Fisrt". This returns only one object result, if no matches returns null.

public static IEntity2 GetConstructionEntity(string Scheme, string Class)
{
    PropertyOccupancyEntity toReturn = 
         (from x in meta.PropertyOccupancy
         where x.Class == Class && x.Scheme == Scheme
         select x).First();

    return toReturn;
}
David Elizondo | LLBLGen Support Team