Search code examples
castle-activerecord

MonoRail - How to grab records where a column isn't null


In MonoRail/Active Record if I want to grab all records that have a certain column equal to null, I can do:

public static Category[] AllParentCategories()
        {
            return (FindAllByProperty("Parent.Id", null));
        }

However, what if I want to grab all records where that column doesn't equal null? I can't figure out how to do that using this FindAllByProperty method, is there another method that is more flexible or a way to grab records using a linq-like querying language?

Thanks, Justin


Solution

  • In NHibernate 2.1 (and by extension, Castle ActiveRecord) you basically have three query APIs you can choose from:

    With Criteria:

    return ActiveRecordMediator<Category>.FindAll(Restrictions.IsNotNull("Parent"));
    

    With Linq:

    return (from c in ActiveRecordLinq.AsQueryable<Category>() 
            where c.Parent != null 
            select c).ToArray();
    

    To get Linq support you'll need NHibernate.Linq.dll and Castle.ActiveRecord.Linq.dll (the latest ActiveRecord release includes everything).