asp.net-mvc-3 - LINQ to Entities 仅支持转换 EDM 基元或枚举类型

标签 asp.net-mvc-3 entity-framework asp.net-mvc-4 entity-framework-4.1 webgrid

我创建了一个 MVC3 应用程序,它使用 WebGrid 填充数据库中的数据。

我的数据库有一些列。说

ID | 全名|电话|电子邮件

我可以对 Id 列进行排序。但如果我尝试对其他列进行排序,则会出现以下错误

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

我尝试调试并查看错误在哪里,我发现排序是完美的,但是当它将数据返回到view时,它给出了错误

我的查看代码:

@{
    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),
    }
    )

这是我在 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);                
}

IStudentInfo接口(interface)中的代码:

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

模型中的代码:

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();
}

我的代码有什么问题?请告诉我是否有人知道发生了什么事。

请帮忙

谢谢

最佳答案

尝试使用:

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();

}

关于asp.net-mvc-3 - LINQ to Entities 仅支持转换 EDM 基元或枚举类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13475162/

相关文章:

c# - Linq从一个表中选择数据而不是在另一个表中

ajax - 使用 MVC 4 API Controller 发布 JSON

wcf - 我可以在 Entity Framework 4.3 中使用 RIA 服务吗?

c# - 如何使用 EF5 包含从一种数据库类型到另一种实体类型的转换?

asp.net-mvc - EF6 Scaffolding/w FK to ApplicationUser 无法正确生成

c# - 使用 Entity Framework 提高效率

c# - MVC4 WebSecurity.CurrentUserId 返回 -1 而 User.Identity.Name 仍然有效

asp.net - 使用 MVC4 自定义错误处理

asp.net-mvc-3 - 将数组传递给 RouteValues 并让它呈现模型绑定(bind)器友好的 url

c# - 使用 [Authorize] 时请求超过配置的 maxQueryStringLength