Nhibernate SQLQuery as Subquery

时间:2018-05-15 12:41:44

标签: nhibernate queryover

如何使用本机sqlquery(session.CreateSqlQuery)作为另一个QueryOver中的过滤子查询:

                // Get  ids
            var idsq = _session.CreateSQLQuery(
                "select Id from [dbo].[SomeFunction](:parameter)")
                .AddEntity(typeof(long)).
                SetParameter("parameter", folderId);

            // get entities by filtering where in (subquery)
            MyEntity entityAlias = null;
            var listOfEntities = await _session.QueryOver(() => entityAlias).
                Where(x=>x.Id).IsIn(idsq).
                OrderBy(x => x.Name).Asc.
                ListAsync(cancelToken).ConfigureAwait(false);

1 个答案:

答案 0 :(得分:3)

你不能轻易混合各种风格的NHibernate ...你能做什么:

var crit = new SQLCriterion(SqlString.Parse("{alias}.Id IN (select Id from [dbo].[SomeFunction](?))"),
                                    new object[] { folderId },
                                    new IType[] { NHibernateUtil.Int64 });

然后:

var listOfEntities = await _session.QueryOver(() => entityAlias)
    .Where(crit)
    .OrderBy(x => x.Name).Asc

请注意我如何更改添加{alias}.Id IN (...)

的文字查询