开发者

Create a list of items within 'var response'

开发者 https://www.devze.com 2023-02-13 13:58 出处:网络
I have the following code: var request = new开发者_开发知识库 GeocodingRequest(); request.Address = postcode;

I have the following code:

var request = new开发者_开发知识库 GeocodingRequest();
request.Address = postcode;
request.Sensor = "false";
var response = GeocodingService.GetResponse(request);

var result = response.Results. ...?

I'd very much like to get result as a list, but I can't seem to convert it. I know I can do something like response.Results.ToList<string>();, but have had no luck.

Can anyone help please :)


Well you can just use:

GeocodingResult[] results = response.Results;

or

List<GeocodingResult> results = response.Results.ToList();

If you want a list of strings, you'll need to decide how you want to convert each result into a string. For example, you might use:

List<string> results = response.Results
                               .Select(result => result.FormattedAddress)
                               .ToList();


It is defined as:

    [JsonProperty("results")]
    public GeocodingResult[] Results { get; set; }

if you want to make it list call: response.Results.ToList().

But why do you want to make it list? You can insert items into list, but I don't think you need it.


assuming response.Results is IEnumerable, just make sure System.Linq is available as a namespace and say response.Results.ToList()

0

精彩评论

暂无评论...
验证码 换一张
取 消