[英]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.