NHibernate的 - queryover - 如何使动态排序的查询加盟(nhibernat

2019-07-30 21:59发布

我有一个用户帐户表管理面板,这台拥有不同的数据库表(在SQL数据库表)的数据。 和用户帐户表必须支持分页和排序。 当我尝试将数据与“姓”从帐户数据库表取回排序,.NET抛出一个异常,说位置数据库表中没有“姓”列。 我的方法是这样的:

        Account acc = null; //bunu bu şekilde tanımlamadan olmuyor :S
        AccountLogin accl = null; //bunu bu şekilde tanımlamadan olmuyor :S
        Program prog = null;
        Location school = null;
        Location town = null;
        Location city = null;

        //Takip edilen bloggerın tüm blog sayfalarını çek
        var query = Session.QueryOver<Account>(() => acc)
            .Left.JoinQueryOver<AccountLogin>(x => x.Logins, () => accl)
            .Left.JoinQueryOver(x => acc.AtentedToProgram, () => prog)
            .Left.JoinQueryOver(x => acc.LiveIn, () => school)
            .Left.JoinQueryOver(x => school.Parent, () => town)
            .Left.JoinQueryOver(x => town.Parent, () => city)
            .Where(x =>                                   acc.CreateDate.IsBetween(DateTime.Now.AddMonths(-12)).And(DateTime.Now)
                   );

        if (program_id != 0)
            query.And(x => prog.program_id == program_id);

        search = search??"";

        string[] searchedTags = search.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

        if (searchedTags != null && searchedTags.Count() > 0)
        {
            Disjunction dis = new Disjunction();

            for (int i = 0; i < searchedTags.Count(); i++)
            {
                dis.Add(Expression.Like(Projections.Property(() => accl.UserName), searchedTags[i], MatchMode.Anywhere));
                dis.Add(Expression.Like(Projections.Property(() => acc.FirstName), searchedTags[i], MatchMode.Anywhere));
                dis.Add(Expression.Like(Projections.Property(() => acc.LastName), searchedTags[i], MatchMode.Anywhere));
                dis.Add(Expression.Like(Projections.Property(() => acc.Email), searchedTags[i], MatchMode.Anywhere));
            }

            query.And(dis);
        }

        if (city_id != 0)
            query.And(x => city.LocationId == city_id);

        if (town_id != 0)
            query.And(x => town.LocationId == town_id);

        if (school_id != 0)
            query.And(x => school.LocationId == school_id);

        var countquery = query.Clone().ToRowCountQuery().FutureValue<int>();

        pager.TotalPageNumber = countquery.Value;

         //acc.GetType().GetProperty(pager.SortColumn)
        var orderred = query.OrderBy(Projections.Property(pager.SortColumn));



            return ordered.Skip(pager.Skip).Take(pager.Take).List();

VAR orderred = query.OrderBy(Projections.Property(pager.SortColumn)); 线路有问题。

这个问题,当我通过pager.SortColumn参数作为字符串的OrderBy方法,它不能知道哪个表有列,并尝试从最后被加入数据库表位置db表得到“名字”。 我该如何解决这个问题? 提前致谢

Answer 1:

您使用JoinQueryOver但在后来的lambda表达式从未使用x。 你可能是为了是JoinAlias创建别名。

.Left.JoinAlias<AccountLogin>(x => x.Logins, () => accl)
.Left.JoinAlias(() => acc.AtentedToProgram, () => prog)
.Left.JoinAlias(() => acc.LiveIn, () => school)
.Left.JoinAlias(() => school.Parent, () => town)
.Left.JoinAlias(() => town.Parent, () => city)


文章来源: nhibernate - queryover - how to make dynamic sorting for joined queries