У меня проблема с возвратом полей, которые я хочу.У меня есть модель, и я хочу вернуть конкретные данные из моей модели.
Вот моя модель.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace api.Models
{
[Table("ScheduleSectionRows")]
public partial class ScheduleSectionRows
{
[Key]
[Column("ScheduleRowID")]
public int ScheduleRowId { get; set; }
[Column("RowID")]
public int? RowId { get; set; }
[Column("StadiumID")]
public int? StadiumId { get; set; }
[Column("SectionID")]
public int? SectionId { get; set; }
[Column("ScheduleID")]
public int? ScheduleId { get; set; }
[Column(TypeName = "decimal(20, 4)")]
public decimal? Amount { get; set; }
public int? AvailableSeats { get; set; }
[StringLength(50)]
public string RowNumber { get; set; }
}
}
По сути, я просто хочу вернуть объект Json, который возвращает заголовокScheduleID и список sectionID этого ScheduleID.
Вот возвращаемые образцы данных из БД
[
{
"ScheduleRowId": 20491,
"RowId": 4559,
"StadiumId": 3,
"SectionId": 81,
"ScheduleId": 43,
"Amount": 100,
"AvailableSeats": 8,
"RowNumber": "7"
},
{
"ScheduleRowId": 20492,
"RowId": 4560,
"StadiumId": 3,
"SectionId": 81,
"ScheduleId": 43,
"Amount": 100,
"AvailableSeats": 10,
"RowNumber": "8"
},
{
"ScheduleRowId": 20493,
"RowId": 4561,
"StadiumId": 3,
"SectionId": 81,
"ScheduleId": 43,
"Amount": 100,
"AvailableSeats": 10,
"RowNumber": "9"
},
{
"ScheduleRowId": 20494,
"RowId": 4562,
"StadiumId": 3,
"SectionId": 81,
"ScheduleId": 43,
"Amount": 100,
"AvailableSeats": 10,
"RowNumber": "10"
},
{
"ScheduleRowId": 20495,
"RowId": 4563,
"StadiumId": 3,
"SectionId": 81,
"ScheduleId": 43,
"Amount": 100,
"AvailableSeats": 10,
"RowNumber": "11"
},
{
"ScheduleRowId": 20496,
"RowId": 4564,
"StadiumId": 3,
"SectionId": 81,
"ScheduleId": 43,
"Amount": 100,
"AvailableSeats": 10,
"RowNumber": "12"
},
{
"ScheduleRowId": 20497,
"RowId": 4565,
"StadiumId": 3,
"SectionId": 81,
"ScheduleId": 43,
"Amount": 100,
"AvailableSeats": 5,
"RowNumber": "13"
},
{
"ScheduleRowId": 20498,
"RowId": 4566,
"StadiumId": 3,
"SectionId": 81,
"ScheduleId": 43,
"Amount": 100,
"AvailableSeats": 10,
"RowNumber": "14"
},
{
"ScheduleRowId": 20499,
"RowId": 4567,
"StadiumId": 3,
"SectionId": 81,
"ScheduleId": 43,
"Amount": 100,
"AvailableSeats": 10,
"RowNumber": "15"
}
]
В основном вот пример вывода, который я хочу получить.
{
"Status" : "Success",
"ScheduleID" : 43,
"SectionID": [
{
"SectionID" : 81,
},
{
"SectionID" : 82,
},
{
"SectionID" : 83,
},
{
"SectionID" : 84,
}
]
}
Вот код, который у меня есть.
public async Task<SectionDTO<ScheduleSectionRows>> GetSection(int scheduleId)
{
var data = _context.MlfbScheduleSectionRows
.Where(s => s.ScheduleId == scheduleId)
.GroupBy(
s => s.SectionId,
( Section) => new { Section = Section})
.ToListAsync();
return new SectionDTO<MlfbScheduleSectionRows>("Success",scheduleId,data);
}
Вот код DTO.
public class SectionDTO<T> where T : class
{
public SectionDTO(string _status,int _scheduleID, IList<T> _data)
{
Status = _status;
ScheduleID = _scheduleID;
Data = _data;
}
public string Status { get; set; }
public int ScheduleID { get; set; }
public IList<T> Data { get; set; }
}