C# 在MVC 5中使用EF选择特定列

C# 在MVC 5中使用EF选择特定列,c#,asp.net-mvc,entity-framework,asp.net-mvc-5,C#,Asp.net Mvc,Entity Framework,Asp.net Mvc 5,我想使用MVC5中的实体框架获得一些特定的列值。但这让我看到了错误。以下是我的控制器方法代码: public ActionResult Index() { var systemUsers = db.SystemUsers .Include(s => s.SystemUser1) .Select(s => new {

我想使用MVC5中的
实体框架
获得一些特定的列值。但这让我看到了错误。以下是我的控制器方法代码:

public ActionResult Index()
{
    var systemUsers = db.SystemUsers
                        .Include(s => s.SystemUser1)
                        .Select(s => new {
                                          s.FullName, 
                                          s.Email, 
                                          s.Image, 
                                          s.UpdateDate, 
                                          s.UpdatedBy, 
                                          s.Id
                                 });
    return View(systemUsers.ToList());
}
这是错误消息:

传入字典的模型项的类型为“System.Collections.Generic.List
1[f_uAnonymousType1
6[System.String,System.String,System.String,System.Nullable
1[System.DateTime],System.Nullable
1[System.Int32],System.Int32],但此词典需要类型为“System.Collections.Generic.IEnumerable`1[MVC.Models.SystemUser]”的模型项

同样,当我无法获得特定列的单个结果时。默认情况下,控制器方法在尝试使用
ajax
获取时也会返回意外的外键数据。 下面是单个结果的代码

[HttpPost]
public ActionResult Details(int? id)
{
    if (id == null)
    {
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    SystemUser systemUser = db.SystemUsers.Find(id);
    return Json(systemUser);
}

视图需要强类型视图模型,但您传递的是匿名类型

更新select以返回强类型对象集合

public ActionResult Index()
{
    var systemUsers = db.SystemUsers
                        .Include(s => s.SystemUser1)
                        .Select(s => new SystemUser { //<--HERE
                                         FullName = s.FullName, 
                                         Email = s.Email, 
                                         Image = s.Image, 
                                         UpdateDate = s.UpdateDate, 
                                         UpdatedBy = s.UpdatedBy, 
                                         Id = s.Id
                                 });
    return View(systemUsers.ToList());
}
public ActionResult Index()
{
var systemUsers=db.systemUsers
.Include(s=>s.SystemUser1)

.Select(s=>newsystemuser{//此处的问题似乎是您的视图希望将
IEnumerable
作为视图模型,但您提供的是一个实例。这就是编译器失败的原因。有几种方法可解决此问题:

1-让您的视图采用
动态
模型:您不必担心传入的实际实例的类型,但是您不会有自动完成或智能感知的细节

2-不要投影(
。选择
)您的收藏,并将原始的
系统用户
列表传递给视图

3-创建视图模型,列出此类视图模型并将其传递给视图:

控制器:

public ActionResult Index()
{
    var systemUsers = db.SystemUsers
                    .Include(s => s.SystemUser1)
                    .Select(s => new SystemUserViewModel 
                            {
                                FullName = s.FullName, 
                                Email = s.Email, 
                                Image = s.Image, 
                                UpdateDate = s.UpdateDate, 
                                UpdatedBy = s.UpdatedBy, 
                                Id = s.Id
                             });
    return View(systemUsers)
}

我想第三个是最好的。你当然应该使用最适合你需要的。希望这个帮助你听说过视图模型吗?google it视图需要强类型视图模型,但是你传递的是匿名类型。@Nkosi,我的代码在哪里编辑?他的代码已经很糟糕了,它在控制器中包含了查询,现在你需要e建议他将查询
SystemUsers
一路发送到视图?这是个好建议吗?我会删除你的第二个建议。@CodingYoshi,lol,我试图提供不同的建议,并试图传达
视图
视图模型
的工作方式,而不是过于固执己见。这就是为什么我建议修正了第三个解决方案,但这不是一个好建议,因此不应建议。