2011-11-16 38 views
0

下面给出的是我的课程。类的返回类型应该是CpsResponse,但我不知道如何把东西放在我的类返回类型中。我对编程世界非常陌生。如何在我的linq查询中返回数据数组作为返回类型?

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; 
      } 
     } 
    } 

我从数据库中获取值到对象'customerData'中。现在我想返回值,我的返回类型应该是CpsResponse。我怎样才能做到这一点?

var customerResult = (from a in customerData 
         select new CpsResponse {//what actually I should //write here I don’t know, Please help}); 

请做好必要的工作。

+0

你应该看看所谓的“自动执行的属性”。它将显着减少你的代码,同时达到相同的结果。 http://msdn.microsoft.com/en-us/library/bb384054.aspx –

+0

不错,您应该将属性初始化代码添加到构造函数中,除非您喜欢空引用异常。 – Maess

回答

2

不知道你CpsResponse的定义,我们不能回答这个问题正好,但基本上它看起来像:

List<CpsResponse> myList = (from a in customerdata 
    select new CpsResonse { id = a.CustomerIdentifier, email = a.emailField }).ToList() 

你做选择新的,与你所需要的领域initalize您CpsResponse的属性DB。

+0

只是注意到你需要一个数组,但是代码在这种情况下非常相似,只需使用ToArray – Pleun