根据子值删除父项

Remove a parent based on child values

我有如下数据结构

public class Prediction
{
    public string Description {get;set;}
    public string Id { get; set; }
    public List<MatchedSubstring> Matched_substrings { get; set; }
    public string Place_id { get; set; }
    public string Reference { get; set; }
    public List<Term> Terms { get; set; }
    public List<string> Types { get; set; }
}

public class GooglePlaceAutocompleteResult
{
    public List<Prediction> Predictions { get; set; }
    public string Status { get; set; }
}

如果 Predictions 列表中的 Types 集合包含字符串 "sublocality".

,我想删除所有项目

如何使用 LINQ 执行此操作?

假设你在GooglePlaceAutocompleteResult内写一个方法,你可以这样写:

Predictions = Predictions.Where(p => !p.Types.Any(t => t.Contains("sublocality")).ToList();

否则代码将是相同的,但会是 result.Predictions = result.Predictions...