English 中文(简体)
MVC3 ajaxgrid scafrafting差,Cannot/2006/10ly换算型系统。 Linq.I Queryable to System.Data.Objects.ObjectQuery
原标题:MVC3 ajaxgrid scaffolding error, Cannot implicitly convert type System.Linq.IQueryable to System.Data.Objects.ObjectQuery

I m, using MVC3 ajaxgrid scafrafting with EF4.1 Code first and i ve this差错:

不可含蓄地转换类型系统。 Linq.I Queryable to System.Data.Objects.ObjectQuery

错误的法典是自动产生的:

public ActionResult GridData(int start = 0, int itemsPerPage = 20, string orderBy = "UserID", bool desc = false)
        {
            Response.AppendHeader("X-Total-Row-Count", repository.Users.Count().ToString());
            ObjectQuery<User> users = (repository as IObjectContextAdapter).ObjectContext.CreateObjectSet<User>();

            users = repository.Users.Include(u => u.Role); //ERROR HERE

            users = users.OrderBy("it." + orderBy + (desc ? " desc" : ""));    
            return PartialView(users.Skip(start).Take(itemsPerPage));
        }

这是用户存放方法以及外国钥匙的作用。

  public IQueryable<Entities.User> Users
        {
            get { return context.Users; }
        }


  public IQueryable<Entities.Role>Roles
        {
            get { return context.Roles; }
        }

如何解决转换问题?

问题回答

清除拉布达并使用相关物体:

var users = repository.Users.Include("Role");  //ERROR HERE

假设实体用户具有航行财产的作用。

理由很清楚:

页: 1 改为ObjectQuery<User>的类型,然后由您指定一个查询结果,即。 IQueryable<User>

UPDATE: Try the code below:

public ActionResult GridData(int start = 0, int itemsPerPage = 20, string orderBy = "UserID", bool desc = false)
{
   Response.AppendHeader("X-Total-Row-Count", repository.Users.Count().ToString());
   //ObjectQuery<User> users = (repository as IObjectContextAdapter).ObjectContext.CreateObjectSet<User>();

   var users = repository.Users.Include(u => u.Role); //ERROR HERE

   users = users.OrderBy("it." + orderBy + (desc ? " desc" : ""));    
   return PartialView(users.Skip(start).Take(itemsPerPage));
}




相关问题
Using jquery to get a partial view and updating the UI

I need to render a partial view (returned from the controller) to show some customer summary details. This will need to happen when the user clicks on a button. In the the mean time the user can ...

MVC 2 / MVC 3 / MVC 4

MVC 2 我们可以轻松地创造领域。 现在,我的问题涉及nes地区(地区内)。

Asp.Net MVC 2 - Changing the PropertyValueRequired string

Using a resx file in the App_GlobalResources directory, I ve been able to change the default message for the PropertyValueInvalid string of the model validators. But it doesn t work to translate the ...

ASP.NET MVC 3 - What features do you want to see? [closed]

I know a bunch of people that are really enjoying the improvements that ASP.NET MVC 2 made over the first release. I have just started to migrate our MVC 1 project over and so far areas has totally ...

热门标签