转换为NHibernate查询

时间:2011-08-24 06:42:12

标签: c# 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

2 个答案:

答案 0 :(得分:1)

我尝试使用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();

我希望它有用!

答案 1 :(得分:1)

如果任何感兴趣的代码如下,我已设法使用自定义投影实现此目的,

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