簡體   English   中英

匹配列表中的字符串並將匹配對象的其他屬性與LINQ一起使用

[英]Match string in list and use other attribute of the matched object with LINQ

我在加油站列表:

public class GasStation
    {
        [JsonProperty("costcentre")]
        public string CostCentre { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }
    }

private IEnumerable<GasStation> gasStationList;
public IEnumerable<GasStation> GasStationList
{
    get => gasStationList;
    set => SetValue(ref gasStationList, value);
}

我有一個要與GasStation對象的CostCentre屬性進行比較的字符串:

GasStationList.Any(x => x.CostCentre == string);

當此匹配為true我想使用此匹配的GasStation並在XAML綁定中顯示其Name

有辦法嗎?

感謝您的幫助

Linq命名空間提供了方法FirstOrDefault<TSource> default<TSource> ,如果枚舉中沒有匹配的項目,則該方法將返回與給定條件匹配的第一項或default<TSource>

GasStation station = gasStationList.FirstOrDefault(x => x.CostCentre == string);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM