Rickshaw
Rickshaw

Reputation: 107

How to return more than one results in my foreach loop

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RestSharp;

namespace Words
{
    public partial class FormWords : Form
    {
        private RestClient client;
        RestRequest request;

        public FormWords()
        {
            InitializeComponent();
        }

        public class RootObject
        {
            public double lng { get; set; }
            public double lat { get; set; }
        }

        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
            client = new RestClient("http://localhost:8080");
            request = new RestRequest("/searchaddr");

            request.AddParameter("addr", textBoxSearch.Text);

            IRestResponse<RootObject> searchResponse = client.Execute<RootObject>(request);

            comboBoxSearch.Show();
            comboBoxSearch.Items.Clear();

            **foreach (var results in searchResponse.Data.lat && searchResponse.Data.lng)**
            {
                comboBoxSearch.Items.Add(results.suggestion.ToString());
                comboBoxSearch.DroppedDown = true;   
            }
        }
    }
}

Thank you for taking the time out to read this. I am trying to return more than one expected result within my API:

I know that's not how you do it, but is there any suggestions that someone can give for me to potentially return the lat and lng in my results?

Upvotes: 0

Views: 183

Answers (3)

Rickshaw
Rickshaw

Reputation: 107

So I made an mistake by not utilising the ToString() function:

var result = searchResponse.Data.lat.ToString();
            {
                textBoxLat.Text = result.ToString();
            }
            var result1 = searchResponse.Data.lng.ToString();
            {
                textBoxLng.Text = result1.ToString();

Upvotes: 0

Sefe
Sefe

Reputation: 14007

The syntax searchResponse.Data.lat && searchResponse.Data.lng is not correct in C#. You might mean one of two things, both of them can be handled with LINQ:

Enumerate through one list and then through the next

Use Enumerable.Concat:

searchResponse.Data.lat.Concat(searchResponse.Data.lng)

Enumerate in parallel through two lists

Use Enumerable.Zip:

searchResponse.Data.lat.Zip(searchResponse.Data.lng, (lat, lng) => new { lat, lng })

Upvotes: 1

Oscar Siauw
Oscar Siauw

Reputation: 483

Is this what you're after? Appending the results from both lat and lng into the combobox items?    

comboBoxSearch.DroppedDown = true;
foreach (var result in searchResponse.Data.lat)
{
    comboBoxSearch.Items.Add(result.suggestion.ToString());
}
foreach (var result in searchResponse.Data.lng)
{
    comboBoxSearch.Items.Add(result.suggestion.ToString());
}

Upvotes: 1

Related Questions