简体   繁体   English

如何在 linq 查询中的 firstordefault 中获取逗号分隔的多个值

[英]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 Exp Op-海得拉巴,孟买

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.我需要一个 for 循环,以便我可以在选定的城市中获得多个值。

 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 .我假设你有一个City类,它有属性SelectedValueDisplayValue and you have deserialized your JSON string to a list.并且您已将 JSON 字符串反序列化为列表。

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.另一种解决方案是从 CSV 中创建一个列表,如x.cities.Split(',').ToList() ,然后使用Contains方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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