C# 使用实体框架数据不匹配

C# 使用实体框架数据不匹配,c#,asp.net-mvc,asp.net-mvc-2,C#,Asp.net Mvc,Asp.net Mvc 2,使用visual studio 2010 express以c语言编写的控制器代码示例- public ActionResult Index() { var _user = Membership.GetUser().ProviderUserKey.ToString(); var profiles = from p in db.Profiles where p.UserId == _user

使用visual studio 2010 express以c语言编写的控制器代码示例-

    public ActionResult Index()
    {
        var _user = Membership.GetUser().ProviderUserKey.ToString();

        var profiles = from p in db.Profiles
                       where p.UserId == _user
                       select new
                       {
                           ProfileId = p.ID
                       };

        return View(profiles.ToList());
    }
来自视图的代码-

@model List<MyProject.Models.Profile>

@{
    ViewBag.Title = "Index";
 }
@型号列表
@{
ViewBag.Title=“Index”;
}
我不断地得到这个错误-

传入字典的模型项的类型为“System.Collections.Generic.List
1[f_uAnonymousType1
1[System.Int32]]”,但此字典需要类型为“System.Collections.Generic.List`1[MyProject.Models.Profile]”的模型项


我做错了什么?

您正在投影到一个匿名对象,就像编译器消息所说的那样。如果您确实想要一个概要文件对象列表,那么应该将linq查询中的select子句替换为simply

 select p;

也就是说,假设db.Profiles包含您的模型对象(model.Profile类型)

您正在投影到一个匿名对象,就像编译器消息所说的那样。如果您确实想要一个概要文件对象列表,那么应该将linq查询中的select子句替换为simply

 select p;
也就是说,假设db.Profiles包含您的模型对象(model.Profile类型)

您应该有

 var profiles = from p in db.Profiles
                       where p.UserId == _user
                       select p;
以满足视图对模型的要求。

您应该

 var profiles = from p in db.Profiles
                       where p.UserId == _user
                       select p;
以满足视图对模型的要求