错误:尝试对ListView进行内部联接时,出现“在方法跳过之前必须调用方法orderby”

时间:2019-01-14 19:38:30

标签: c# asp.net sql-server listview inner-join

目标:从两个不同的SQL Server表中获取信息以显示到ListView中。大部分来自User表,一个值来自UserRole表(尤其是“ Description”字段)。

我一直在解决一系列错误。当前的是: “仅在linq到实体的排序输入中支持方法跳过。必须在方法跳过之前调用方法orderby” 正如所有SO帖子所述,我已经尝试将OrderBy添加到查询中,但是仍然出现错误。

问题:(OrderBy)是否应该在其他地方?而且我会选择正确的方向还是是否有更好的实现将两个表连接到单个ListView中?

aspx:

    <asp:ListView id="lstUsers" runat="server" ItemType="Project.Classes.UserWRole" DataKeyNames="ID" SelectMethod="listUsers_GetData"
    OnItemCommand="lstUsers_ItemCommand" DeleteMethod="lstUsers_DeleteItem" OnSorting="lstUsers_Sorting"
     OnItemDataBound="lstUsers_ItemDataBound"
    ItemPlaceholderID="litPlaceHolder">

    <LayoutTemplate>
        <div class="table-responsive">
            <table class="table">
                <thead>
                    <tr>                            
                         <th>Username</th>
                        <th>TAX ID</th>
                        <th>Role</th>
                    </tr>
                </thead>
                <asp:Literal ID="litPlaceHolder" runat="server" />
            </table>
        </div>
    </LayoutTemplate>

    <ItemTemplate>
        <tr>                
            <td><%# Item.UserName %></td>
            <td><%# Item.Tax_ID %></td>
            <td> <%# Item.Description %></td>
        </tr>
    </ItemTemplate>

default.aspx.cs:

public IQueryable<UserWithRole> listUsers_GetData()
        {
            IQueryable<User> users = Project.Classes.Helper.UsersForUser(
                db: db,
                UserID: SessionManager.CurrentUserID,
                Search_User: Search_User,
                aUserRoleShortcut: SessionManager.CurrentUserRole
                ).OrderBy(i=>i.UserRoleID);



            IQueryable<UserRole> userRoles = db.UserRoles.OrderBy(i=>i.ID);

            IQueryable<UserWRole> myQuery = from user in users
                        join userRole in userRoles on user.UserRoleID equals userRole.ID
                        select new UserWRole{ ID = user.ID, LastName = user.LastName, FirstName = user.FirstName, OrgName = user.OrgName, UserName = user.UserName, Description = userRole.Description, Tax_ID = user.Tax_ID, DateAdded = user.DateAdded };


            if (users != null)
            {
                listNumSearchResults.Text = string.Format("{0} Results Found", users.Count());
                if (users.Count() > 10)
                {
                    ddlRecordsPerPage.Visible = true;
                    ltItemsPerPage.Visible = true;
                    ddlRecordsPerPage2.Visible = true;
                    ltItemsPerPage2.Visible = true;
                    dp1.Visible = true;
                }                   
            }

            return myQuery;
        }

UserWithRole.cs:

namespace Project.Classes
{
    public class UserWRole
    {
        public string LastName, FirstName, OrganizationName, UserName, Description, Tax_ID;
        public DateTime DateAdded;
        public int ID;
    }
}

helper.cs:

public static IQueryable<d.User> UsersForUser(d.ProjectPE db, int UserID, string Search_User, e.UserRoleEnum? aUserRoleShortcut)
        {
            IQueryable<d.User> users = null;                

            users = db.Users.OrderBy(a => a.LastName).AsQueryable();

            if (!string.IsNullOrEmpty(Search_User))
            {
                users = users.Where
                     (a =>
                          a.LastName.ToLower().StartsWith(Search_User.ToLower()) ||
                          a.FirstName.ToLower().StartsWith(Search_User.ToLower()) ||
                          a.UserName.ToLower().StartsWith(Search_User.ToLower())
                     ).AsQueryable();
            }

            users = users.Where(a => a.IsActive).AsQueryable();
            return users;
        }           

我希望看到第二个表中的列显示在ListView中,但是却出现了这些错误

1 个答案:

答案 0 :(得分:0)

The following resolved the issue:

https://docs.microsoft.com/en-us/dotnet/csharp/linq/order-the-results-of-a-join-clause

I usually do the method syntax, and didn't realize I was formatting the orderby incorrectly with the syntax for this join. I put the orderby in between the join and select and it works now:

join uR in uRs on u.URID equals uR.ID
                    orderby uR.ID
                    select new UserWRole