我正在使用 SearchBar,我可以返回一些 API JSON 數(shù)據(jù)來(lái)查找公司名稱及其股票代碼。這會(huì)被很好地反序列化為 ObservableCollection 并顯示在 ListView 中。但是,有時(shí)結(jié)果包含帶有“-”(破折號(hào))的股票代碼。我想以某種方式測(cè)試 ObservableCollection 中的每個(gè)項(xiàng)目,并阻止符號(hào)名稱中包含“-”的任何項(xiàng)目,然后將其排除在綁定到 ListView 的 Collection 中。這是我的搜索對(duì)象: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 中包含“-”符號(hào)的 SearchObject 項(xiàng)的最佳方法是什么?換句話說(shuō),我不希望任何帶有包含破折號(hào)的符號(hào)的項(xiàng)目顯示在 ListView 中。任何有關(guān)正確語(yǔ)法的指導(dǎo)將不勝感激。
1 回答

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