nhibernate - queryover - how to make dynamic sorting for joined queries

送分小仙女□ 提交于 2019-12-09 20:05:48

问题


I have a user accounts table in admin panel and this table holds data from different db tables (tables in SQL database ). And user accounts table has to support paging and sorting. When I try to sort data with "FirstName" fetched from Account db table , .net throw an exception saying Location db table does not have "FirstName" column. My method is like:

        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)); line has problem.

The problem, when I pass pager.SortColumn parameter as string to OrderBy method, it cant know which table has the column and try to get "Firstname" from Location db table that is last joined db table. How can I solve this ? Thanks in advance


回答1:


you use JoinQueryOver but never use the x in the lambdas afterwards. What you probably intended was JoinAlias to create the alias.

.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)


来源:https://stackoverflow.com/questions/11889980/nhibernate-queryover-how-to-make-dynamic-sorting-for-joined-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!