使用linq从数据库中检索配置文件页面 [英] Retrieving Profile page from database with linq

查看:75
本文介绍了使用linq从数据库中检索配置文件页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试获取当前用户的个人资料页面,但我在屏幕上看到错误,我不知道它来自哪里。拜托,我需要任何对这个问题有明确答案的程序员的答案。详见以下内容:



错误:

传入字典的模型项目是类型System.Linq.Enumerable + WhereSelectListIterator`2 [Community.Models.Person.Profile,f__AnonymousType5`11 [System.String,System.String,System.String,System.String,System.String,System.String,System。 DateTime,System.String,System.Int32,System.Int32,System.Int32]],但此字典需要System.Collections.Generic.IEnumerable`1 [Community.Models.Person.Profile]类型的模型项。 />




我在个人资料控制器中的代码:

[授权]

public ActionResult Proffe()

{

var profiles =(来自d中的db.Profiles.ToList()

其中d .Username == User.Identity.Name

选择新{

FirstName = d.FirstName,

LastName = d.LastName,

地址= d.Address,

Email = d.Email,

Phone = d.Phone,

Country = d.Country ,

DateOfBirth = d.DateOfBirth,

描述= d.Description,

ProfileId = d.ProfileId,

Comment = d.Comments.Count,

Post = d.Posts.Count< i>< / i>

})。AsEnumerable();





返回查看(个人资料);

}

解决方案

1)创建一个强类型的类



  public  < span class =code-keyword> class  ProfileEntity 
{
public string FirstName { get ; set ; }
public string LastName { get ; set ; }
public string 地址{ get ; set ; }
// 添加所有属性
}





2)创建 ProfileEntity 类的新列表为



  var  profiles =( from  d < span class =code-keyword> in  db.Profiles.ToList()
其中 d.Username == User.Identity.Name
选择 new ProfileEntity(){Address = d.Address,FirstName = d.FirstName} ).ToList(); // 添加所有属性





注意:在视图中,您必须使用模型作为 ProfileEntity


Hello, i am trying to get the profile page of the current user but i am seeing an error on my screen and i don't know where it seems to be coming from. please, i need answers from any programmer that has a definite answer to this question. see below for details:

ERROR:
The model item passed into the dictionary is of type System.Linq.Enumerable+WhereSelectListIterator`2[Community.Models.Person.Profile,f__AnonymousType5`11[System.String,System.String,System.String,System.String,System.String,System.String,System.DateTime,System.String,System.Int32,System.Int32,System.Int32]], but this dictionary requires a model item of type System.Collections.Generic.IEnumerable`1[Community.Models.Person.Profile].


My code in profile controller:
[Authorize]
public ActionResult Proffe()
{
var profiles = (from d in db.Profiles.ToList()
where d.Username == User.Identity.Name
select new{
FirstName = d.FirstName,
LastName = d.LastName,
Address = d.Address,
Email = d.Email,
Phone = d.Phone,
Country = d.Country,
DateOfBirth = d.DateOfBirth,
Description = d.Description,
ProfileId = d.ProfileId,
Comment = d.Comments.Count,
Post = d.Posts.Count<i></i>
}).AsEnumerable();


return View(profiles);
}

解决方案

1) Create a strongly typed class

public class ProfileEntity
   {
       public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
// add all the properties
   }



2) create a new list of the ProfileEntity class as

var profiles = (from d in db.Profiles.ToList()
where d.Username == User.Identity.Name
select new ProfileEntity() { Address  = d.Address , FirstName =d.FirstName }).ToList(); // add all the properties



note: in the view, you have to use the model as ProfileEntity


这篇关于使用linq从数据库中检索配置文件页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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