我在blazor服务器端工作,当从web Api返回数据时,我面临问题。
我的问题,我不能返回数组的对象雇员id没有请求的详细信息。
但是我只需要返回对象employee id
的数组,不需要请求状态详细信息
id,exception,status,isCanceled,isCompleted,isCompletedSuccessfully,creationOptions,asyncState,isFaulted
,不需要这些细节与数据一起发送
那么如何只返回雇员id对象的数组而不需要上面的详细信息请求呢?
[HttpGet]
[Route("GetAllEmployee/{searchText}")]
public async Task<IActionResult> GetAllEmployee(string searchText)
{
string query = "";
query = "SELECT cast(EMP.YAAN8 as varchar(20)) as EmployeeID,EMP.YAALPH AS EmployeeName FROM CRPDTA.F060116 EMP WHERE cast(EMP.YAAN8 as varchar(20)) LIKE '%" + searchText + "%' WITH UR";
try
{
ICollection<object> ApplicationsDataValues = new List<object>();
using (var command = con.CreateCommand())
{
command.CommandText = query;
command.CommandType = CommandType.Text;
await command.Connection.OpenAsync();
using (var reader = await command.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
ApplicationsDataValues.Add(new
{
EmployeeID = reader.GetFieldValueAsync<string>(0)
});
}
}
}
await con.CloseAsync();
return StatusCode(200, ApplicationsDataValues); // Get all users
}
catch (Exception e)
{
return StatusCode(500, e);
}
}
当前返回的结果
[
{
"employeeID": {
"result": "212334",
"id": 251,
"exception": null,
"status": 5,
"isCanceled": false,
"isCompleted": true,
"isCompletedSuccessfully": true,
"creationOptions": 0,
"asyncState": null,
"isFaulted": false
}
},
{
"employeeID": {
"result": "212331",
"id": 252,
"exception": null,
"status": 5,
"isCanceled": false,
"isCompleted": true,
"isCompletedSuccessfully": true,
"creationOptions": 0,
"asyncState": null,
"isFaulted": false
}
}
]
预期结果返回如下
[
{
"employeeID": "212334"
},
{
"employeeID": "212331"
}
]
GetFieldValueAsync
方法返回Task<T>
的值。您需要应用await
来获取该值。
ApplicationsDataValues.Add(new
{
EmployeeID = await reader.GetFieldValueAsync<string>(0)
});
或者,您可以查找GetFieldValue
方法来获取值。
ApplicationsDataValues.Add(new
{
EmployeeID = reader.GetFieldValue<string>(0)
});