English 中文(简体)
• 如何使用“Cind One()”方法
原标题:How to use Castle ActiveRecords FindOne() method

我有一个简单的一类。

    [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; }
}

我想采用从基类继承下来的“Find One()”方法进行简单研究。 它喜欢使用NHibernate。 危机。 可是,我可以找到任何例子。

最佳回答

它很复杂:

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

你的言辞是你需要做的一切事情,才能打造你的雕像。

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

Greetings Juy Juka

问题回答

你们也可以使用LINQ,我发现LINQ syntax可以读得更远:

// 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);

如果贵阶层继承现役证书,你可以简单写:

// 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);




相关问题
Many-To-Many Surrogate Key problem. Please help!

I have a many-to-many relationship with surrogate key The classes are: Insurer - InsurerSection - Section. InsurerSection has one extra attribute: active : bool. How do I access these bool ...

hql get objects where certain property is unique

I am trying to perform an hql query which returns a list of objects with distinct property value. Following is my pseudo code: string hql = @"select distinct m from Merchandise m where ...

nhibernate hql subquery performance

I have written an hql to support paging string hql = @"select distinct mr from MediaResource as mr where mr.Deleted= false ...

ASP.NET MVC - Castle ActiveRecord - Show SQL queries

I m using ASP.NET MVC with Castle ActiveRecord as my persistance layer. I want to know if it s possible to show the SQL queries being executed on my MySQL server. I know it s possible in a Web ...

热门标签