簡體   English   中英

轉換為NHibernate查詢

[英]Convert to NHibernate 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

我試圖用QueryOver和Alias解決它; 抱歉,我現在無法測試此代碼。 :(

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

我希望它有用!

我已設法使用自定義投影實現此目的,如果任何感興趣的代碼如下,

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

在這里,投影的構造函數需要通過投影傳遞所有組,如,

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