nhibernate - queryover - 如何对连接的查询进行动态排序

时间:2012-08-09 18:45:56

标签: c# nhibernate fluent-nhibernate queryover

我在管理面板中有一个用户帐户表,该表包含来自不同数据库表(SQL数据库中的表)的数据。并且用户帐户表必须支持分页和排序。当我尝试使用从Account db表中获取的“FirstName”对数据进行排序时,.net会抛出一个异常,说位置db表没有“FirstName”列。我的方法就像:

        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表的位置db表中获取“Firstname”。我怎么解决这个问题?提前致谢

1 个答案:

答案 0 :(得分:1)

你使用JoinQueryOver但之后从未在lambdas中使用过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)