简体   繁体   中英

how to get comma separated multiple values in firstordefault in linq query

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;
});

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM