如何在我的linq查询中返回数据数组作为返回类型?

时间:2011-11-16 10:25:20

标签: c# asp.net linq linq-to-entities

以下是我的课程。类的返回类型应该是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});

请做好必要的事。

1 个答案:

答案 0 :(得分:2)

在不知道CpsResponse的定义的情况下,我们无法准确回答这个问题,但基本上它会是这样的:

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

您可以从数据库中选择新的并使用您需要的字段初始化CpsResponse的属性。