亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 JSON ObservableCollection 中過濾掉項目,使它們不顯示在列表中?

如何從 JSON ObservableCollection 中過濾掉項目,使它們不顯示在列表中?

C#
繁花不似錦 2023-07-22 18:39:06
我正在使用 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 將包含不具有“-”字符的符號的項目。


查看完整回答
反對 回復 2023-07-22
  • 1 回答
  • 0 關注
  • 195 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號