Home > Blockchain >  how to get comma separated multiple values in firstordefault in linq query
how to get comma separated multiple values in firstordefault in linq query

Time:10-18

i have a cities names. while getting single its coming but while its having more than one its not coming.

Exp Op- Hyderabad,Mumbai

error message: firstOrdefault error

x.cities = HYD,MUM;
var citieslst =  [{
        "SelectedValue": "HYD",
        "DisplayValue": "hyderbad"
      
    },
    {
        "SelectedValue": "MUM",
        "DisplayValue": "Mumbai"
       
    }]

i need a for loop so that multiple values i can get in the selected cities.

 lstRecords.ForEach(x =>
 {
   x.SelectedCities = citieslst.FirstOrDefault(ch => ch.SelectedValue == x.cities).DisplayValue;
});

CodePudding user response:

you can do this.

I am assuming you have a City class that has properties SelectedValue and DisplayValue. and you have deserialized your JSON string to a list.

List<City> ls = new List<City>();
foreach(var item in lstRecords)
{
    if(x.cities.Contains(item.SelectedValue)
    {
        ls.Add(item);
    }
}

One more solution could be to create a List from the CSV like x.cities.Split(',').ToList() and then use the Contains method.

  • Related