我在返回想要的字段时遇到问题。我有一个模型,我想从模型中返回特定数据。
这是我的模特。
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的标头和该ScheduleID的sectionID的列表。
这是从数据库返回样本数据
[
{
"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; }
}
答案 0 :(得分:2)
首先,结果看起来需要返回唯一的SectionID。可以改写为:
var query = _context.MlfbScheduleSectionRows
.Where(s => s.ScheduleId == scheduleId)
.Select(s => s.SectionId)
.Distinct();
var data = await query.ToListAsync();
ToListAsync()
本身不返回数据,而是返回一个Task,该任务将在完成后返回数据。需要await
来等待该任务并获取数据。
SQL查询如下所示:
SELECT DISTINCT SectionID
FROM ScheduleSectionRows
WHERE ScheduleId = @p0
结果是一个List<int?>
,它将被序列化为JSON:
[ 11, 23, 43 ]
要获得所需的结果,我们必须转换返回仅包含单个SectionID
属性的类型。 LINQ的.Select()
可以轻松返回匿名类型,例如data.Select(id=>new {SectionId = id})
,但是我们需要一个实际的“命名”类型来传递给SectionDTO
。
public class Section
{
public int? SectionID {get;set;}
}
....
public async Task<SectionDTO<Section>> GetSection(int scheduleId)
{
var query = _context.MlfbScheduleSectionRows
.Where(s => s.ScheduleId == scheduleId)
.Select(s => s.SectionId)
.Distinct();
var data = await query.ToListAsync();
var sections=data.Select(id=>new Section{SectionID=id})
.ToList();
return new SectionDTO<Section>("Success",scheduleId,sections);
}
答案 1 :(得分:-1)
您必须根据您的json响应创建c#模型,
public class SectionID
{
public int SectionID { get; set; }
}
public class RootObject
{
public string Status { get; set; }
public int ScheduleID { get; set; }
public List<SectionID> SectionID { get; set; }
}
然后您可以使用Linq .select
填充此模型。
答案 2 :(得分:-1)
在将groupby
与引用类型一起使用时,请确保您覆盖equals
和gethashcode
方法。这些用于生成分组实体的“密钥”。
在下面的代码中,我删除了where T : class
上的约束SectionDTO
,并使用了将键设置为int(非引用类型)的方法。它正在将值分组。
//using Newtonsfot.Json library
//using it to get the list data below, not required for the solution
JArray j = JArray.Parse(GetJSONString());
//Select the schedule id/section id as a list
var list = j.Select(x => new { ScheduleId= Convert.ToInt32(x["ScheduleId"]) ,SectionID = Convert.ToInt32(x["SectionId"]) });
//group by into a SectionDTO object
var result = list.GroupBy(x => x.ScheduleId, (key, value) => new SectionDTO<int> { Status = "Success", ScheduleID = key, Data = value.Select(x=>x.SectionID).ToList() });
//if you want to convert it to a json string then use the convert method
string JSonString = JsonConvert.SerializeObject(result);