简体   繁体   中英

NHibernate not sorting by a nullable child property

I have this method to get paged and filtered data:

public IList<T> GetFiltered(int startIndex, int rowCount, Expression<Func<T, bool>> where,
        Dictionary<Expression<Func<T, object>>, object> like, List<DataColumn> dataColumns, string search,
        out int count)
    {
        IList<T> result = null;

        using (ITransaction transaction = this._session.BeginTransaction())
        {
            _session.DisableFilter("ActiveVersionFilter");
            _session.DisableFilter("AtivoFilter");

            ICriteria criteria = this._session.CreateCriteria<T>();

            //Add filters...
            criteria.Add(Restrictions.Where(where));

            //Add sorting and Likes
            Junction disjunction = Restrictions.Disjunction();
            foreach (var dataColumn in dataColumns)
            {
                if (dataColumn.Name.Contains("."))
                {
                    var alias = dataColumn.Name.Split('.').First();
                    criteria.CreateCriteria(alias, alias, JoinType.LeftOuterJoin);
                }

                if (dataColumn.Searchable)
                {
                    ICriterion criterion = Restrictions.InsensitiveLike(dataColumn.Name, search, MatchMode.Anywhere);
                    disjunction.Add(criterion);
                }

                if (dataColumn.IsOrdered)
                    criteria.AddOrder(new Order(dataColumn.Name, dataColumn.Ascending));
            }

            //Add likes..
            if (!disjunction.ToString().Equals("()")) criteria.Add(disjunction);

            //clone to count
            var resultCriteria = (ICriteria) criteria.Clone();

            //Add paging...
            resultCriteria.SetFirstResult(startIndex).SetMaxResults(rowCount);

            result = resultCriteria.List<T>();
            count = criteria.SetProjection(Projections.RowCount()).FutureValue<int>().Value;

            transaction.Commit();
        }

        return result;
    }

My problem is with the "Add sorting and likes" section. I pass a list of "DataColumn" object with the sorted and searchable columns (I'm using jQuery DataTables plugin for this). Some sortable columns are child property, so I set the alias and the column name joined with a dot (ie "documento.numero") and split to get only the alias to add the Criteria (left join).

The problem happens when I sort by this colum "documento.numero" and the "document" is null. The "left join" seems not to be working and return only the entries with a valid "documento".

Am I doing something wrong? Do I have to provide more details?

Thanks!

UPDATE

For the generated SQL I have two scenarios:

  1. Before clicking (loads the defaults) - first load of the page: (PS: I'm just copying the WHERE and ORDERBY section)

    WHERE ( ( ( this_.status = 1 and ( 1 = 1 or this_.locIid = 1 ) ) and this_.ativo = 'T' ) and this_.activeVersion = 0 ) ORDER BY this_.criacao asc, documento1_.numero asc )

  2. After clicking on column "documento.numero":

    WHERE ( ( ( this_.status = 1 and ( 1 = 1 or this_.locIid = 1 ) ) and this_.ativo = 'T' ) and this_.activeVersion = 0 ) and ( lower(documento1_.numero) like '%%' ) ORDER BY this_.criacao asc, documento1_.numero asc )

Note that the only differente is the addition of the " and ( lower(documento1_.numero) like '%%' ) " section. That happens because this column is both sortable and searchable. How can I say to NHibernate to search in this column but ignore the null ones? I think that would solve my problem.

Found the solution myself

Just added an extra check in the line:

if (dataColumn.Searchable)

Added:

if (dataColumn.Searchable && !string.IsNullOrWhiteSpace(search))

Well, I figured out that the problem occour when the seach is empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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