【问题标题】:How to return array of data as return type in my linq query?如何在我的 linq 查询中返回数据数组作为返回类型?
【发布时间】: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


【解决方案1】:

在不知道您的CpsResponse 的定义的情况下,我们无法准确回答这个问题,但基本上它看起来像:

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

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

【讨论】:

  • 刚刚注意到您需要一个数组,但在这种情况下代码非常相似,只需使用 ToArray
猜你喜欢
  • 2012-12-13
  • 2011-08-20
  • 1970-01-01
  • 1970-01-01
  • 2010-12-08
  • 2018-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多