Cannot apply an object of type "System.Data.Entity.Infrastructure.DbQuery`1 []" using the lqda linq expression

I am new to mvc and trying to understand what I am doing wrong. I create a classDbcontaxt

 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 view with a strong type

    [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; }

}
}

in the controller, I am trying to create a Details view by adding a view and choosing AddUserToRoleas my model for a strict 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);
    }

he gives me this error

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'.

and when I quit return View((UsersInRoles)UserInRole);, he gives me this error

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

and presentation

@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?

+4
source share
1 answer

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

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

: pjotr FirstOrDefault() SingleOrDefault()

+13

Source: https://habr.com/ru/post/1535408/


All Articles