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]
I get a stack overflow error from the last line of the below code. I cant see why. Any ideas?
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();
My class;
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;}
}
}
}
My table (With example data)
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里:
注意到 getter 和递归调用中缺少
_
了吗?应该是:
顺便说一句,自从我开始使用 自动实现的属性在 C# 中,这些类型的错误被遗忘了:
只是一个挑剔的旁注:属性名称以大写字母开头是一种很好的编码习惯(
TimeSlot
而不是timeSlot
) 。Here:
Notice the missing
_
in the getter and the recursive call?Should be:
And by the way since I started to use Auto implemented properties in C# those kind of errors went into oblivion:
Just a nitpicking side note: it is good coding practice to have property names start with a capital letter (
TimeSlot
instead oftimeSlot
).这是你的递归。这里需要一个
_
:get { return this.timeSlot; }
Here's your recursion. You need a
_
here:get { return this.timeSlot; }