views:

47

answers:

1

Hey,

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

A: 

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).

Mauricio Scheffer
Thanks but I wasn't able to get either approach working. I don't have a FindAll method that accepts a Restrictions object as a parameter. Regarding Linq, I referenced CastleActiveRecord.Linq and downloaded NHibernate Linq 1 (NHibernate Linq 2.1 said it was an invalid zip file) and referenced it too. The project builds fine but then at run-time I get:Could not load file or assembly 'NHibernate.Linq, Version=1.0.0.0, Culture=neutral, PublicKeyToken=444cf6a87fdab271' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.
Justin
@Justin: what version of ActiveRecord are you using?
Mauricio Scheffer
The Castle.ActiveRecord.dll and Cast.ActiveRecord.Linq.dll say version 1.9.201.0. I was able to open the latest NHibernate.Linq dll by using winrar instead of winzip but it still gives the same error.
Justin
I downloaded the latest MonoRail and ActiveRecord releases and was able to work through the build errors and now the linq code works, thanks!!Last thing, can you copy/paste me what the FindAll(Restrictions) method would look like? I will most likely use the linq for everything but I want to keep my options open.
Justin
`ActiveRecordMediator<Category>.FindAll(Restrictions.IsNotNull("Parent"))` that's it.
Mauricio Scheffer
What I'm saying though is that I don't have a FindAll method with a Restrictions parameter so I can't do that, the only one I have is:public static T[] FindAll(DetachedCriteria criteria) { return ActiveRecordMediator<T>.FindAll(criteria); }
Justin
@Justin: sure you do. The method's signature is `public static T[] FindAll(params NHibernate.Criterion.ICriterion[] criterias)`
Mauricio Scheffer
`Restrictions.IsNotNull("Parent")` returns an `AbstractCriterion`, which implements `ICriterion`
Mauricio Scheffer

related questions