无法使用linq lambda表达式转换类型为'System.Data.Entity.Infrastructure.DbQuery`1 []'的对象 [英] Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[]' using linq lambda expression

查看:78
本文介绍了无法使用linq lambda表达式转换类型为'System.Data.Entity.Infrastructure.DbQuery`1 []'的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mvc的新手,我试图弄清我做错了什么,所以我创建了一个Dbcontaxt

i'm new to mvc and trying to get idea of what i'm doing wrong i create a Dbcontaxt class

 public class DataBaseContext : DbContext
{
    public DataBaseContext()
        : base("DefaultConnection")
    {
    }
    public DbSet<Membership> Membership { get; set; }
    public DbSet<OAuthMembership> OAuthMembership { get; set; }
    public DbSet<Role> Roles { get; set; }
    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<Category> Categorys { get; set; }
    public DbSet<SubCategory> SubCategorys { get; set; }
    public DbSet<Product> Products { get; set; }
    public DbSet<Color> Colors { get; set; }
    public DbSet<Size> Sizes { get; set; }
    public DbSet<Company> Companys { get; set; }
    public DbSet<UsersInRoles> UsersInRoles { get; set; }
  }
}

我创建了一个模型类来创建一个强类型视图

and i create a model class to create a strongly type view

    [Bind(Exclude = "AddUserToRoleID")]
    public class AddUserToRole
{
    [ScaffoldColumn(false)]
    public int AddUserToRoleID { get; set; }
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }
    [Required]
    [Display(Name = "Role name")]
    public string RoleName { get; set; }

}
}

在控制器中,我试图通过添加视图来创建详细信息"视图,然后选择AddUserToRole作为强类型视图的模型

in the controller i'm trying to create the Details view by adding view and select AddUserToRole as my model for the strongly type view

    public ActionResult Details(int id = 0)
    {

        var UserInRole = db.UserProfiles
        .Join(db.UsersInRoles,
              u => u.UserId,
              uir => uir.UserId,
              (u, uir) => new {u = u,uir = uir})
        .Join(db.Roles,
              temp0 => temp0.uir.RoleId,
              r => r.RoleId,
              (temp0, r) => new { temp0 = temp0,r = r })
        .Where(temp1 => (temp1.temp0.u.UserId == id))
        .Select(temp1 => new AddUserToRole {
            AddUserToRoleID = temp1.temp0.u.UserId,
            UserName = temp1.temp0.u.UserName,
            RoleName = temp1.r.RoleName
        });            

        return View(UserInRole);
    }

它给我这个错误

The model item passed into the dictionary is of type
'System.Data.Entity.Infrastructure.DbQuery`1[SeniorProject.Models.AddUserToRole]', but
this dictionary requires a model item of type 'SeniorProject.Models.AddUserToRole'.

当我投射return View((UsersInRoles)UserInRole);时,它给了我这个错误

and when i cast return View((UsersInRoles)UserInRole); it give me this error

Unable to cast object of type
'System.Data.Entity.Infrastructure.DbQuery`1[SeniorProject.Models.AddUserToRole]' 
 to type'SeniorProject.Models.UsersInRoles'.

和视图

@model SeniorProject.Models.AddUserToRole

@{
ViewBag.Title = "Details";
}

<h2>Details</h2>

<fieldset>
    <legend>AddUserToRole</legend>

<div class="display-label">
     @Html.DisplayNameFor(model => model.UserName)
</div>
<div class="display-field">
    @Html.DisplayFor(model => model.UserName)
</div>

<div class="display-label">
     @Html.DisplayNameFor(model => model.RoleName)
</div>
<div class="display-field">
    @Html.DisplayFor(model => model.RoleName)
</div>
</fieldset>
<p>
@Html.ActionLink("Edit", "Edit", new { id=Model.AddUserToRoleID }) |
@Html.ActionLink("Back to List", "Index")
</p>

在这种情况下我该怎么办?

what should i do in this case?

推荐答案

您需要实现它.在查询末尾放置FirstOrDefault()

You need to materialize it. Put FirstOrDefault() at the end of the query

var materializedUser = UserInRole.SingleOrDefault();
return View(materializedUser);

遵循pjotr注释,将FirstOrDefault()替换为SingleOrDefault()

Following pjotr comment replacing FirstOrDefault() with SingleOrDefault()

这篇关于无法使用linq lambda表达式转换类型为'System.Data.Entity.Infrastructure.DbQuery`1 []'的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆