Search code examples
asp.net-mvc-3entity-frameworkasp.net-mvc-4entity-framework-4.1webgrid

LINQ to Entities only supports casting EDM primitive or enumeration types


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.Pager(WebGridPagerModes.NextPrevious)
        @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("intID","SId",canSort:true),
        grid.Column("strFirstName","Name",canSort:true,format:(item)=>item.strFirstName+"   "+item.strLastName),
        grid.Column("strPhone","Phone",canSort:true),
        grid.Column("strEmail","Email",canSort:true),
    }
    )

Here is my code in controller:

public readonly IStudentInfo _istudentrepository; 

public studentController( IStudentInfo _iStudentRepository)
{
    this._istudentrepository = _iStudentRepository;
}

public ActionResult listBidder(string sort, string sortdir, int? page)
{
    int startPage = 0;
    IEnumerable<Students> sList;
    if (page.HasValue && page.Value > 0)
    {
        startPage = page.Value;
    }
    sList = _istudentrepository.GetList(startPage, PageSize, sort, sortdir);
    return View(sList);                
}

Code in Interface IStudentInfo:

public interface IStudentInfo
{
    IEnumerable<Students> GetList(int intPage, int intRecords, string strSort, string sortdir);            
}

Code in Model:

private MyEntity _entity;

public StudentListRepository(MyEntity Ent)
{
    this._entity = Ent;
}
public IEnumerable<Students> GetList(int intPage, int intRecords, string strSort, string sortdir)
{

    var finalresult = new Students();
    var bidList = (from userInfo in _entity.tbl_UserInf
                   join user in _entity.tbl_User on userInfo.UserId equals user.UserId
                   select new Students()
                   {
                       intID=user.UserId,
                       strFirstName = user.FirstName,
                       strEmail = userInfo.EmailId,
                       intPhone=userInfo.Phone
                   }).OrderByDescending(m => m.intID);
    finalresult.TotalResult = bidList.Count();
    switch (strSort)
    {
        case "intID":
            if (sortdir == "ASC")
            {
                sList = sList.OrderBy(r => r.Id);
            }
            else
            {
                sList= sList.OrderByDescending(r => r.Id);
            }
            break;
        case "strFirstName":
            if (sortdir == "ASC")
            {
                sList = sList.OrderBy(r => r.strFirstName);
            }
            else
            {
                sList= sList.OrderByDescending(r => r.strFirstName);
            }
            break;
        case "strEmail":
            if (sortdir == "ASC")
            {
                sList = sList.OrderBy(r => r.strEmail);
            }
            else
            {
                sList= sList.OrderByDescending(r => r.strEmail);
            }
            break;
        //repeat same for phone
    }

    finalresult.lstStudents = sList.Skip(intPage * intRecords)
                                   .Take(intRecords)
                                   .ToList();
    return sList.AsEnumerable();
}

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

Please help

thanks,


Solution

  • Try to use:

    public ActionResult listBidder(string sort, string sortdir, int? page)
    {
            int startPage = 0;
            if (page.HasValue && page.Value > 0)
            {
                startPage = page.Value;
            }
            var sList = _istudentrepository.GetList(startPage, PageSize, sort, sortdir);
            return View(sList);
    }
    
    public IEnumerable<Students> GetList(int intPage, int intRecords, string strSort, string sortdir)
    {
    
            var finalresult = new Students();
            var bidList = (from userInfo in _entity.tbl_UserInf
                           join user in _entity.tbl_User on userInfo.UserId equals user.UserId
                           select new Students()
                           {
               intID=user.UserId,
                               strFirstName = user.FirstName,
                               strEmail = userInfo.EmailId,
                               intPhone=userInfo.Phone
                           }).OrderByDescending(m => m.intID);
           finalresult.TotalResult = bidList.Count();
           // There are some sorting and ordering
           finalresult.lstStudents = sList.Skip(intPage * intRecords).Take(intRecords).ToList();
           return bidList.ToArray();
    

    }