Home > Back-end >  Compare string to array
Compare string to array

Time:10-06

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"
          ]
        }
      ]
    }
  ]
}

CodePudding user response:

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>

CodePudding user response:

You can do a simple LINQ query like so, it'll select the cities first, then query them to get only the set where a postcode matches your input, "50000" in this case, then select the PSHid, and then finally return it as a list.

data.state
    .SelectMany(x => x.city)
    .Where(x => x.postcode.Any(y => y == "50000")))
    .Select(x => x.PSHid)
    .ToList();
  • Related