Question

I have a simple class like this

    [ActiveRecord("Subscriptions")]
    public class Subscription : ActiveRecordBase<Subscription>
    {
        public Subscription()
        {

        }

        public Subscription(string name)
        {
            this.Name = name;
        }

        [PrimaryKey(PrimaryKeyType.Native)]
        private int Id { get; set; }

        [Property]
        public string Name { get; set; }
}

And I want to do a simple lookup using the FindOne() method that's inheritd from the base class. It looks like it uses NHibernate.Criterion.DetachedCriteria but I can't find any examples.

Was it helpful?

Solution

it isn't very complex:

Subscription.FindOne(NHibernate.Criterion.Expression.Eq("Id",3))

With the Expression-class you have all the things you need to build your sql/hql/select, you just have to nest it again and again.

Subscription.FindOne(Expression.And(Expression.Eq(...),Expression.Eq(...)))

Greetings Juy Juka

OTHER TIPS

You can also use LINQ, I find the LINQ syntax much more readable:

// find the subscription with id = 3
var subscription = Castle.ActiveRecord.Framework.ActiveRecordLinqBase<Subscription>.Queryable.SingleOrDefault(s => s.Id == 3);

// find all the active subscriptions
var activeSubscriptions = ActiveRecordLinqBase<Subscription>.Queryable.Where(s => s.IsActive);

If your class inherits from ActiveRecordLinqBase you can simply write:

// find the subscription with id = 3
var subscription = Subscription.Queryable.SingleOrDefault(s => s.Id == 3);

// find all the active subscriptions
var activeSubscriptions = Subscription.Queryable.Where(s => s.IsActive);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top