2

我在管理面板中有一个用户帐户表,该表包含来自不同数据库表(SQL 数据库中的表)的数据。并且用户帐户表必须支持分页和排序。当我尝试使用从 Account db 表中获取的“FirstName”对数据进行排序时,.net 会抛出异常,指出 Location 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 表的 Location db 表中获取“Firstname”。我该如何解决这个问题?提前致谢

4

1 回答 1

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)
于 2012-08-14T06:03:21.357 回答