Tuesday 22 January 2013

How to fix Error The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.

Watch online
I am getting the following error
The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.

I have simple web app with gridview.  data is loaded through entity 
SelectMethod="GetDepartments"
            ItemType="Model_Binding.Dpt"


        private SchoolEntities db = new SchoolEntities();
        public IQueryable<Dpt> GetDepartments()
        {
            var query = this.db.Dpts
              .Include(c => c.Users);
            return query;
        }
 

Of course I allow sorting and paging - therefore that error.
To fix it add DataKeyNames

        <asp:GridView ID="GridView1" runat="server" SelectMethod="GetDepartments"
            ItemType="Model_Binding.Dpt" AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns="False" DataKeyNames="DepartmentId" PageSize="1">

No comments:

Post a Comment