0

NHibernate バージョン: 3.3.1

元のクエリは次のとおりです。

List<PostInstruction> instructions = PostInstruction.Query(
    Restrictions.Eq("Campaign", campaign),
    Restrictions.In("Status", new PostInstructionStatus[] { 
                PostInstructionStatus.Pending, 
                PostInstructionStatus.Finished 
                }),
    Restrictions.Or(
        Restrictions.Gt("DateCreated", DateTime.UtcNow - TimeSpan.FromMinutes(15)),
        Restrictions.IsNotNull("Post")
        )
    );

カウント機能にしか使用していなかったので、カウントのみを返すように変換しましたが、問題が発生しました。null の Post 値を考慮するために、2 つの別々のクエリを実行する必要があります。単一のクエリで次のことを行う方法はありますか?

    public static int CountByCampaignCity(Campaign campaign, City city)
    {
        int finished, pending;

        using (ISession session = DataAccess.SessionFactory.OpenSession())
        using (ITransaction transaction = session.BeginTransaction())
        {
            try
            {
                Type objectType = typeof(PostInstruction);
                ICriteria criteria = session.CreateCriteria(objectType);

                criteria.Add(Restrictions.Eq("Campaign", campaign));
                criteria.Add(Restrictions.Eq("City", city));
                criteria.Add(Restrictions.Eq("Status", PostInstructionStatus.Finished));

                criteria.CreateCriteria("Post").Add(Restrictions.In("Status", new PostStatus[] { PostStatus.Pending, PostStatus.Live }));
                criteria.SetProjection(Projections.CountDistinct("Id"));

                finished = int.Parse(criteria.UniqueResult().ToString());
            }
            catch
            {
                if (transaction != null)
                    transaction.Rollback();

                throw;
            }
        }

        using (ISession session = DataAccess.SessionFactory.OpenSession())
        using (ITransaction transaction = session.BeginTransaction())
        {
            try
            {
                Type objectType = typeof(PostInstruction);
                ICriteria criteria = session.CreateCriteria(objectType);

                criteria.Add(Restrictions.Eq("Campaign", campaign));
                criteria.Add(Restrictions.Eq("City", city));
                criteria.Add(Restrictions.Eq("Post", null));
                criteria.Add(Restrictions.Eq("Status", PostInstructionStatus.Pending));
                criteria.Add(Restrictions.Gt("DateCreated", DateTime.UtcNow - TimeSpan.FromMinutes(15)));
                criteria.SetProjection(Projections.CountDistinct("Id"));

                pending = int.Parse(criteria.UniqueResult().ToString());
            }
            catch
            {
                if (transaction != null)
                    transaction.Rollback();

                throw;
            }
        }

        return pending + finished;
    }
4

1 に答える 1

0

QueryOver を使用すると、次のようなことができるはずです。

var instructionsCount = session.QueryOver<PostInstruction>()
    .Select(Projections.Count<PostInstruction>(pi => pi.Id))
    .Where(pi => pi.Campaign == campaign && pi.City == city && (pi.Post == PostStatus.Pending || pi.Post == PostStatus.Live || pi.Post == null)); //Add your other conditions

Console.WriteLine(instructionsCount.SingleOrDefault<int>()); 
于 2013-02-04T06:44:04.503 に答える