我正在使用 SearchBar,我可以返回一些 API JSON 數據來查找公司名稱及其股票代碼。這會被很好地反序列化為 ObservableCollection 并顯示在 ListView 中。但是,有時結果包含帶有“-”(破折號)的股票代碼。我想以某種方式測試 ObservableCollection 中的每個項目,并阻止符號名稱中包含“-”的任何項目,然后將其排除在綁定到 ListView 的 Collection 中。這是我的搜索對象:public class SearchObject { public string symbol { get; set; } public string securityName { get; set; } public string securityType { get; set; } public string region { get; set; } public string exchange { get; set; } }這是我的工作 JSON API 代碼: vSearchSymbol = mySearchBar.Text; SearchUrl = string.Format("MY SEARCH API URL {0}", vSearchSymbol); // Activity indicator visibility ON activity_indicator.IsRunning = true; // Getting JSON data from the Web var content = await _client.GetStringAsync(SearchUrl); // Deserialize the JSON data from content var tr = JsonConvert.DeserializeObject<List<SearchObject>>(content); // After deserializing, we store our data in an ObservableCollection List called 'trends' ObservableCollection<SearchObject> trends = new ObservableCollection<SearchObject>(tr); // Bind the list to the ListView myList.ItemsSource = trends; // Check the number of Items in the Observable Collection int i = trends.Count; if (i > 0) { // If the list count is > 0 then stop activity indicator activity_indicator.IsRunning = false; }消除 ObservableCollection 中包含“-”符號的 SearchObject 項的最佳方法是什么?換句話說,我不希望任何帶有包含破折號的符號的項目顯示在 ListView 中。任何有關正確語法的指導將不勝感激。
1 回答

桃花長相依
TA貢獻1860條經驗 獲得超8個贊
我希望我正確理解了你的意思。一種更簡單的方法是在使用 LINQ 創建 ObservableCollection 之前過濾掉 SearchObject。例如,
ObservableCollection<SearchObject> trends = new ObservableCollection<SearchObject>( tr.Where(x=>!x.symbol.Contains("-")));
現在,您的 ObservableCollection 將包含不具有“-”字符的符號的項目。
- 1 回答
- 0 關注
- 195 瀏覽
添加回答
舉報
0/150
提交
取消