我有以下Asp.Net 4.5 Web Api方法:
public IEnumerable<RCWindsWCFService.uspGetAllStationsResult> GetExtAllStations()
{
RCWindsWCFService.Service1Client client = new RCWindsWCFService.Service1Client();
IEnumerable<RCWindsWCFService.uspGetAllStationsResult> results = client.GetExtAllStations();
return results;
}
返回以下JSON结果:
[{POINT_X:-81.0410610591,POINT_Y:34.1831858023,TABLE_NAME:“Cedar Creek”},{POINT_X:-80.7653777161,POINT_Y:33.8641198907,TABLE_NAME:“Gadsden”}]
我需要返回的是:
{Station_Coordinates:[{POINT_X:-81.0410610591,POINT_Y:34.1831858023,TABLE_NAME:“Cedar Creek”},{POINT_X:-80.7653777161,POINT_Y:33.8641198907,TABLE_NAME:“Gadsden”}]}
由于我对这些技术不熟悉,我希望能够就如何实现这些技术提出建议。如果可能,对上述代码的特定建议更改将有所帮助。
谢谢
为了改善我对@ L.B评论的回复格式,我将重新发布:
public IEnumerable<RCWindsWCFService.uspGetAllStationsResult> GetExtAllStations()
{
RCWindsWCFService.Service1Client client = new RCWindsWCFService.Service1Client();
StationCoordinates coordinates = new StationCoordinates();
IEnumerable<RCWindsWCFService.uspGetAllStationsResult> results = client.GetExtAllStations();
coordinates.Station_Coordinates = results;
return coordinates;
}
class StationCoordinates
{
public IEnumerable<RCWindsWCFService.uspGetAllStationsResult> Station_Coordinates { set; get; }
}
我现在收到: 无法将类型'RCWindsExtSvc.Controllers.RCWindsExtSvcController.StationCoordinates'隐式转换为'System.Collections.Generic.IEnumerable'。存在显式转换(您是否错过了演员?) - 我怀疑我的使用不当。
提前再次感谢你。
更新
代码已经过修改,以反映Moby Disk的评论如下:
public IEnumerable<StationCoordinates> GetExtAllStations()
{
RCWindsWCFService.Service1Client client = new RCWindsWCFService.Service1Client();
StationCoordinates coordinates = new StationCoordinates();
//IEnumerable<coordinates> results = client.GetExtAllStations();
IEnumerable<RCWindsWCFService.uspGetAllStationsResult> results = client.GetExtAllStations();
coordinates.Station_Coordinates = results;
return coordinates;
}
class StationCoordinates
{
public IEnumerable<RCWindsWCFService.uspGetAllStationsResult> Station_Coordinates { set; get; }
}
错误代码仍然存在:
无法将类型'RCWindsExtSvc.Controllers.RCWindsExtSvcController.StationCoordinates'隐式转换为'System.Collections.Generic.IEnumerable'。存在显式转换(您是否错过了演员?)
答案 0 :(得分:0)
L.B。的回答对我来说似乎很合适:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace Server.Controllers
{
public class TempController : ApiController
{
public StationCoordinates GetExtAllStations()
{
RCWindsWCFService.Service1Client client = new RCWindsWCFService.Service1Client();
StationCoordinates coordinates = new StationCoordinates();
IEnumerable<RCWindsWCFService.uspGetAllStationsResult> results = client.GetExtAllStations();
coordinates.Station_Coordinates = results;
return coordinates;
}
public class StationCoordinates
{
public IEnumerable<RCWindsWCFService.uspGetAllStationsResult> Station_Coordinates { set; get; }
}
}
}
namespace RCWindsWCFService
{
public class uspGetAllStationsResult
{
public double POINT_X { get; set; }
public double POINT_Y { get; set; }
public string TABLE_NAME { get; set; }
}
public class Service1Client
{
public IEnumerable<uspGetAllStationsResult> GetExtAllStations()
{
return new List<uspGetAllStationsResult>()
{
new uspGetAllStationsResult() {POINT_X= -81.0410610591,POINT_Y= 34.1831858023,TABLE_NAME= "Cedar Creek"},
new uspGetAllStationsResult() {POINT_X= -80.7653777161,POINT_Y= 33.8641198907,TABLE_NAME= "Gadsden"}
};
}
}
}
上面的JSON输出是:
{&#34; Station_Coordinates&#34;:[{&#34; POINT_X&#34;: - 81.0410610591,&#34; POINT_Y&#34;:34.1831858023,&#34; TABLE_NAME&#34;:&#34 ;雪松 溪&#34;},{&#34; POINT_X&#34;: - 80.7653777161,&#34; POINT_Y&#34;:33.8641198907,&#34; TABLE_NAME&#34;:&#34;加兹登&#34;}]}
我认为这就是你想要的。