构造从实体相似的对象模型 [英] Constructor for object model similar from entity
问题描述
我想我最好遵循一些MVC准则和现在,我已经创建了我从我有一个实体需要领域的典范。我创建了以下模型类:
I'm trying my best to follow some MVC guidelines and for now, I have created a model with the fields I need from an entity I have. I have created the following Model class:
public class PersonStyle
{
public string Name { get; set; }
public int? Age { get; set; }
public string City { get; set; }
public string Style { get; set; }
}
我的实体是sometihng这样的:
My Entity is sometihng like:
public class PersonOE
{
public string Name { get; set; }
public int? Age { get; set; }
public string City { get; set; }
}
我试着去建立以下的构造:
Im trying to build a constructor for the following:
PersonON personBus = new personBus();
List<PersonStyle> personStyleList = new List<PersonStyle>(personBus.getPeople()); //getPeople(); returns a PersonOE list
对于这一切,我需要关于如何创建 PersonStyle
构造,将放在空到从 PersonOE唯一不同的变量建议
模式。
For this all, I need suggestions on how to create the PersonStyle
constructor that will put "null" into the only different variable from the PersonOE
model.
推荐答案
所有你需要做的是使用LINQ生成您的属性。虽然,将您样式到他们每个人可以得到一个有点难以对付,但你没有给他们来自何处的任何输入。这是从你的数据转换为模拟对象的一个快速简便的方法。
All you need to do is use LINQ to generate your properties. Though, adding your Style to each of them could get a little harder to deal with, but you haven't given any input on where they're coming from. This is a quick and easy way to convert from your data to model objects.
List<PersonStyle> personStyleList = personBus.GetPeople()
.Select(p => new PersonStyle {
Name = p.Name,
Age = p.Age,
City = p.City
});
这篇关于构造从实体相似的对象模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!