ToArray() 的堆栈溢出

发布于 2024-11-24 01:23:48 字数 2203 浏览 2 评论 0原文

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

        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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人迟暮 2024-12-01 01:23:48

这里:

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

注意到 getter 和递归调用中缺少 _ 了吗?

应该是:

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

顺便说一句,自从我开始使用 自动实现的属性在 C# 中,这些类型的错误被遗忘了:

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

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

Here:

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

Notice the missing _ in the getter and the recursive call?

Should be:

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

And by the way since I started to use Auto implemented properties in C# those kind of errors went into oblivion:

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

Just a nitpicking side note: it is good coding practice to have property names start with a capital letter (TimeSlot instead of timeSlot).

花心好男孩 2024-12-01 01:23:48

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

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

Here's your recursion. You need a _ here: get { return this.timeSlot; }

    public string timeSlot
    {
        get { return this.timeSlot; }
        set {_timeSlot = value;}
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文