How to get a list from a SQL server using sqlcontext in C#

4c8rllxm  于 2023-05-05  发布在  SQL Server
关注(0)|答案(1)|浏览(140)

i'm currently maintaining a C# based back end system and i'm in need to get a list of objects from the sql server from a simple id, but the entire system is done through sqlcontext actions, unfortunately i cant seem to find a good response to what i need. Is there a good way to get this list of objects using that? All i need is to return all the entries that have this id on their eventId column.

this is a example of what the system do, this is a function that returns one entry, but i'm not sure if it will do what i want:

public async Task<EventDate> GetById(int id)
{
  return await _sqlContext.EventDate
    .Include(x => x.Hours)
    .Include(x => x.Event)
    .Where(x => x.Id == id)
    .AsNoTracking()
    .FirstOrDefaultAsync();
}

i would be grateful if someone could explain that to me a little more, i'm not so experienced in that regard and i cant seem to find any good explanations.

Edit: Due to a request, here is the create script for the EventDate table:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[EventDate](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [IdEvent] [int] NOT NULL,
    [IdZoomAccount] [int] NULL,
    [Date] [datetime2](7) NOT NULL,
    [Hour] [time](7) NULL,
    [Type] [int] NOT NULL,
    [Url] [nvarchar](max) NULL,
    [MeetingType] [int] NOT NULL,
    [DurationHours] [nvarchar](max) NULL,
    [DurationMinutes] [nvarchar](max) NULL,
    [IdRoom] [int] NULL,
    [IdLocation] [int] NULL,
    [EventType] [int] NOT NULL,
 CONSTRAINT [PK_EventDate] 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] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[EventDate] ADD  DEFAULT ((0)) FOR [MeetingType]
GO

ALTER TABLE [dbo].[EventDate] ADD  DEFAULT ((0)) FOR [IdLocation]
GO

ALTER TABLE [dbo].[EventDate] ADD  DEFAULT ((0)) FOR [EventType]
GO

ALTER TABLE [dbo].[EventDate]  WITH CHECK ADD  CONSTRAINT [FK_EventDate_MeetingLocation_IdLocation] FOREIGN KEY([IdLocation])
REFERENCES [dbo].[MeetingLocation] ([Id])
GO

ALTER TABLE [dbo].[EventDate] CHECK CONSTRAINT [FK_EventDate_MeetingLocation_IdLocation]
GO

ALTER TABLE [dbo].[EventDate]  WITH CHECK ADD  CONSTRAINT [FK_EventDate_Event_IdEvent] FOREIGN KEY([IdEvent])
REFERENCES [dbo].[Event] ([Id])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[EventDate] CHECK CONSTRAINT [FK_EventDate_Event_IdEvent]
GO

ALTER TABLE [dbo].[EventDate]  WITH CHECK ADD  CONSTRAINT [FK_EventDate_Rooms_IdRoom] FOREIGN KEY([IdRoom])
REFERENCES [dbo].[Rooms] ([Id])
GO

ALTER TABLE [dbo].[EventDate] CHECK CONSTRAINT [FK_EventDate_Rooms_IdRoom]
GO

ALTER TABLE [dbo].[EventDate]  WITH CHECK ADD  CONSTRAINT [FK_EventDate_ZoomAccount_IdZoomAccount] FOREIGN KEY([IdZoomAccount])
REFERENCES [dbo].[ZoomAccount] ([Id])
GO

ALTER TABLE [dbo].[EventDate] CHECK CONSTRAINT [FK_EventDate_ZoomAccount_IdZoomAccount]
GO
y1aodyip

y1aodyip1#

Thanks to all that commented on my question, @CodeCaster was the one to solve my issue, all i needed to do was create a new function as follows:

public async Task<List<EventDate>> GetListById(int id)
{
   return await _sqlContext.EventDate
   .Where(x => x.IdEvent == id)
   .AsNoTracking()
   .ToListAsync();
}

相关问题