【发布时间】:2011-11-16 10:25:20
【问题描述】:
以下是我的课程。类的返回类型应该是 CpsResponse,但我不知道如何将东西放在我的类返回类型中。我对编程世界很陌生。
public class CpsResponse
{
private CustomerProfile[] customerProfileField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("CustomerProfile")]
public CustomerProfile[] CustomerProfile
{
get
{
return this.customerProfileField;
}
set
{
this.customerProfileField = value;
}
}
}
/// <remarks/>
public class CustomerProfile
{
private CustomerIdentifier customerIdentifierField;
private string emailField;
private string titleField;
private string firstNameField;
/// <remarks/>
public CustomerIdentifier CustomerIdentifier
{
get
{
return this.customerIdentifierField;
}
set
{
this.customerIdentifierField = value;
}
}
/// <remarks/>
public string Email
{
get
{
return this.emailField;
}
set
{
this.emailField = value;
}
}
/// <remarks/>
public string Title
{
get
{
return this.titleField;
}
set
{
this.titleField = value;
}
}
/// <remarks/>
public string FirstName
{
get
{
return this.firstNameField;
}
set
{
this.firstNameField = value;
}
}
}
/// <remarks/>
public class CustomerIdentifier
{
private string uniqueCustomerIdentifierField;
private string versionField;
/// <remarks/>
public string UniqueCustomerIdentifier
{
get
{
return this.uniqueCustomerIdentifierField;
}
set
{
this.uniqueCustomerIdentifierField = value;
}
}
/// <remarks/>
public string Version
{
get
{
return this.versionField;
}
set
{
this.versionField = value;
}
}
}
我正在从 db 获取值到对象“customerData”中。现在我想返回值,我的返回类型应该是 CpsResponse。我怎样才能做到这一点?
var customerResult = (from a in customerData
select new CpsResponse {//what actually I should //write here I don’t know, Please help});
请做必要的事情。
【问题讨论】:
-
您应该研究一种叫做“自动实现的属性”的东西。它将显着减少您的代码,同时获得相同的结果。 msdn.microsoft.com/en-us/library/bb384054.aspx
-
好点,但是,您应该将属性初始化代码添加到构造函数,除非您喜欢空引用异常。
标签: c# asp.net linq linq-to-entities