LINQ to Entities only supports casting EDM primitive or enumeration types RRS feed

  • Question

  • I have created a MVC3 application, which uses `WebGrid` to populate the data from the database.

    My database has some columns. Say 

    Id | FullName|Phone|Email

    I can able to sort the `Id` column. But if i try to sort the other columns, it is giving the following error

        Unable to cast the type 'MvcApp.Models.Student' to type 'MvcApp.Models.Student'. LINQ to Entities only supports casting EDM primitive or enumeration types.

    I tried to debug and see where the error is, i found that the sorting is perfect, but when it returns the data to `view` it gives error

    My View Code:

            ViewBag.Title = "listStudents";
            Layout = "~/Views/Shared/_Layout.cshtml";
            WebGrid grid = new WebGrid(Model, canPage: true, canSort: true, rowsPerPage: 3); 

                @grid.GetHtml(  //Error at this line
                htmlAttributes: new { id = "grdListStudents" },
            fillEmptyRows: true,
            headerStyle: "tblHeader",
            tableStyle: "tablestyle",
            mode: WebGridPagerModes.All,
            firstText: "<< First",
            previousText: "< Previous", nextText: "Next >",
            lastText: "Last >>",
            columns: new[]{
                grid.Column("strFirstName","Name",canSort:true,format:(item)=>item.strFirstName+"   "+item.strLastName),

    intID,strFirstName,strPhone,strEmail are the properties.

    Update: My sortdir=ASC remains as-is when the webgrid header is clicked. Only ASC is being performed.

    What is wrong in my code ? Please tell me if anyone has idea of what is going on.

    Please help




    • Moved by CoolDadTx Wednesday, November 21, 2012 3:52 PM EF related (From:Visual C# General)
    • Edited by Karthik999 Thursday, November 22, 2012 4:40 AM
    Wednesday, November 21, 2012 7:09 AM

All replies

  • I have a similar problem implementing a generic repository patter to query for entity with orderby, using EF5.

     public virtual IQueryable<T> GetAll(params Expression<Func<T, object>>[] orderProperties)        
      var query = this._repository.All;            
      IOrderedQueryable<T> ordered = null;            
      int propertyCount = 0;            
      foreach (var p in orderProperties)
       if (++propertyCount == 1)                     
        ordered = query.OrderBy(p);                 
        ordered = ordered.ThenBy(p);            
      if (ordered != null)                
        query = ordered.AsQueryable();             
       return query;       

    then the calling code looks like this:

    var armQry = this.armsService2.GetAll(x => x.ArmId);
    //the following commented line works perfectly but the above throws exception
    //var armQry = this.armsService2.GetAll(x => x.Name);
    var armsPaged = armQry.ToPagedList(page, this.PageSize); //the exception is thrown here
    var armsMapped = mappingService.Map<IEnumerable<Arm>, IEnumerable<ArmModel>>(armsPaged.ToArray());
    var viewModel = new StaticPagedList<ArmModel>(armsMapped, armsPaged.GetMetaData());
    ViewBag.PageSize = this.PageSize;
    return View(viewModel);

    Here is the error message: Unable to cast the type 'System.Int32' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types.

    • Edited by Kunle Smart Wednesday, November 21, 2012 10:03 AM
    Wednesday, November 21, 2012 10:01 AM
  • I'm not really sure why this is throwing an exception. Are one, or both, of you able to provide me with a solution that reproduces the problem?

    If not then I will try and repro it myself, and see if I can get a dev involved that worked on it. But a repro would make things go much faster.

    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Wednesday, November 21, 2012 7:27 PM