简体   繁体   English

转换为NHibernate查询

[英]Convert to NHibernate Queryover

How do I convert following SQL to NH Queryover, 如何将以下SQL转换为NH Queryover,

select COUNT(*)
from
(select p.CODE,sl.BATCH from STORELOCATION sl
right join PRODUCT p on p.CODE = sl.CODE
group by p.CODE,sl.BATCHID)
as t

I've tried to solve it using QueryOver and Alias; 我试图用QueryOver和Alias解决它; sorry, but I can't test this code now. 抱歉,我现在无法测试此代码。 :( :(

        ProductModel myProd = null;
        StoreLocationModel myLocation = null;

        var qOver = _HibSession.QueryOver<ProductModel>(() => myProd)
            .JoinAlias(() => myProd.Locations, () => myLocation, JoinType.LeftOuterJoin)
            .Select(Projections.GroupProperty(myProd.CODE), Projections.GroupProperty(myLocation.BATCHID))
            .RowCount();

I hope it's helpful! 我希望它有用!

I have managed to achieve this using a custom projection, if anyone interested code is as follows, 我已设法使用自定义投影实现此目的,如果任何感兴趣的代码如下,

[Serializable]
    public class GroupCountProjection : SimpleProjection
    {
        private PropertyProjection[] _projections;

        public GroupCountProjection(PropertyProjection[] projections)
        {
            _projections = projections;
        }

        public override bool IsAggregate
        {
            get { return true; }
        }

        public override IType[] GetTypes(ICriteria criteria, ICriteriaQuery criteriaQuery)
        {
            return new IType[] { NHibernateUtil.Int32 };
        }

        public override SqlString ToSqlString(ICriteria criteria, int position, ICriteriaQuery criteriaQuery, IDictionary<string, IFilter> enabledFilters)
        {
            SqlStringBuilder result = new SqlStringBuilder()
                .Add(" count(*) as y")
                .Add(position.ToString())
                .Add("_ from ( select ");
            for (int index = 0; index < _projections.Length; index++)
            {
                PropertyProjection projection = _projections[index];
                if (index > 0)
                    result.Add(",");
                result.Add(projection.ToSqlString(criteria, ++position, criteriaQuery, enabledFilters));
            }
            result.Add(" ");
            return result.ToSqlString();
        }

        public override string ToString()
        {
            return "select count(*)";
        }

        public override bool IsGrouped
        {
            get { return true; }
        }

        public override SqlString ToGroupSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery,
                                                   IDictionary<string, IFilter> enabledFilters)
        {
            SqlStringBuilder result = new SqlStringBuilder();
            for (int index = 0; index < _projections.Length; index++)
            {
                PropertyProjection projection = _projections[index];
                if (index > 0)
                    result.Add(",");
                result.Add(StringHelper.RemoveAsAliasesFromSql(projection.ToSqlString(criteria, 0, criteriaQuery,enabledFilters)));
            }
            result.Add(") as tbly");
            return result.ToSqlString();
        }
    }

here, the constructor of the projection needs to be passed with all the group by projections like, 在这里,投影的构造函数需要通过投影传递所有组,如,

var countQuery = GetProductQuery(); // this is the queryover 
            countQuery
                .Select(new GroupCountProjection(new[]{
                    Projections.Group(() => _productAlias.Code),
                    Projections.Group(() => _storeLocationAlias.Batch),
                 }));
int resultCount = (int)countQuery.List<object>().SingleOrDefault();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM