Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Compare string to array

I want to compare a string (postcode, e.g:"50000") to array of string elements and if the array contain the postcode stated, it need to select the "PSHid" of the array. I managed to deserialized the Json file but not I’m stuck how to compare my postcodes. Here is my code:

using Newtonsoft.Json;
using System.Threading.Tasks;
using System.Net.Http;

namespace ConsoleApp1
{

    public class Program
    {
        static async Task Main(string[] args)
        {
            string url = "https://raw.githubusercontent.com/kayh1105/msiaPostcodes/main/ShortList_MsiaCitiesPostcodes";
            HttpClient httpClient = new HttpClient();

            var httpResponseMessage = await httpClient.GetAsync(url);
            string jsonResponse = await httpResponseMessage.Content.ReadAsStringAsync();
            Console.WriteLine(jsonResponse);
            var data = JsonConvert.DeserializeObject<MsiaPostcode>(jsonResponse);            
        }
    }
}

public class MsiaPostcode
{
    public _State[] state { get; set; }
}

public class _State
{
    public string name { get; set; }
    public _City[] city { get; set; }
}

public class _City
{
    public string name { get; set; }
    public int PSHid { get; set; }
    public string[] postcode { get; set; }
}

and below is my Json sample:

{
  "state": [
    {
      "name": "Wp Kuala Lumpur",
      "city": [
        {
          "name": "Kuala Lumpur",
          "PSHid": 1,
          "postcode": [
            "50000",
            "50050"
          ]
        },
        {
          "name": "Setapak",
          "PSHid": 2,
          "postcode": [
            "53300"
          ]
        }
      ]
    },
    {
      "name": "Johor",
      "city": [
        {
          "name": "Ayer Baloi",
          "PSHid": 3,
          "postcode": [
            "82100"
          ]
        },
        {
          "name": "Ayer Hitam",
          "PSHid": 4,
          "postcode": [
            "86100"
          ]
        },
        {
          "name": "Yong Peng",
          "PSHid": 5,
          "postcode": [
            "83700",
            "83710"
          ]
        }
      ]
    }
  ]
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

First flatten the list of states (many to many relationship : Many states, each state can have multiple cities) into list of cities.

After getting list of cities filter by Postcode,

To Flatten the list of list into one sequential list

SelectMany(): Projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence.

var PSHids = data.state
    .SelectMany(x => x.city) //Convert list of states into list of cities
    .Where(x => x.postcode.Contains("50000")))  //Filter by postcode
    .Select(x => x.PSHid) //Select PSHid property only
    .ToList();   //Convert IEnumerable<int> to List<int>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading