使用ToArray堆栈溢出()

时间:2011-07-14 15:32:35

标签: c# linq-to-sql stack-overflow

我从下面代码的最后一行得到了堆栈溢出错误。我不明白为什么。有什么想法吗?

        var slots = from a in db.AvailableAppointments
                    where a.RequestID == reqId
                    select new
                    DataLayer.DateAndTimeslot
                    {
                        date = a.Date.ToShortDateString(),
                        timeSlot = a.Timeslot
                    };

        returnValue.DateAndTimeslot = slots.ToArray();

我的班级;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace DataLayer
{
    [DataContract(Namespace = "http://wholesale.fluidata.co.uk/bt/AppointmentAvailabilityResponse")]

    public class AppointmentAvailabilityResponse : DataLayer.WebserviceMessage
    {
        DateAndTimeslot[] _dateAndTimeSlot;

        [DataMember]
        public DateAndTimeslot[] DateAndTimeslot
        {
            get { return _dateAndTimeSlot; }
            set { _dateAndTimeSlot = value; }
        }

    }

    public class DateAndTimeslot
    {
        private String _date;
        private String _timeSlot;

        [DataMember]
        public string date 
        {
            get { return this._date; }
            set {_date = value;}
        }

        [DataMember]
        public string timeSlot
        {
            get { return this.timeSlot; }
            set {_timeSlot = value;}
        }

    }
}

我的表格(带示例数据)

ID  RequestID   Date            Timeslot
171 3214    2005-12-28 00:00:00.000 EV
172 3214    2005-12-28 00:00:00.000 EV
173 3214    2005-12-29 00:00:00.000 EV
174 3214    2005-12-29 00:00:00.000 EV
175 3214    2005-12-30 00:00:00.000 EV
176 3214    2005-12-30 00:00:00.000 EV


CREATE TABLE [dbo].[AvailableAppointments](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [RequestID] [int] NOT NULL,
    [Date] [datetime] NOT NULL,
    [Timeslot] [varchar](21) NOT NULL,
 CONSTRAINT [PK_AvalibleAppointments] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

2 个答案:

答案 0 :(得分:7)

下面:

[DataMember]
public string timeSlot
{
    get { return this.timeSlot; }
    set { _timeSlot = value; }
}

注意getter和递归调用中缺少_

应该是:

[DataMember]
public string timeSlot
{
    get { return this._timeSlot; }
    set { _timeSlot = value; }
}

顺便说一下,自从我开始在C#中使用Auto implemented properties以来,这些错误就被遗忘了:

[DataMember]
public string TimeSlot { get; set; }

只是一个挑剔的旁注:将属性名称以大写字母(TimeSlot而不是timeSlot)开头是一种很好的编码习惯。

答案 1 :(得分:3)

这是你的递归。您需要_get { return this.timeSlot; }

    public string timeSlot
    {
        get { return this.timeSlot; }
        set {_timeSlot = value;}
    }