2011-07-14 285 views
2

我從下面的代碼的最後一行得到一個堆棧溢出錯誤。我不明白爲什麼。有任何想法嗎?堆棧溢出ToArray()

 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] 

回答

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

+1這是使用性能就會給追趕前面這一個更好的機會的資本駱駝。 – spender

+0

完美謝謝:) –

3

這是你的遞歸。您需要這裏_get { return this.timeSlot; }

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