简体   繁体   English

Linq ObservableCollection没有返回结果

[英]Linq ObservableCollection return no result

I am using linq to search product in ObservableCollection, but it does not return any result, for example I have a product name "stack", but when I search "s", it return no result: 我正在使用linq来搜索ObservableCollection中的产品,但是它没有返回任何结果,例如我有一个产品名称“stack”,但是当我搜索“s”时,它没有返回结果:

private ObservableCollection<Product> _ProductList = new ObservableCollection<Product>();

public ObservableCollection<Product> ProductList 
 { get { return _ProductList; } set { _ProductList = value; } }

I have a textbox, when user type, linq will search product name in the ObservableCollection: 我有一个文本框,当用户输入时,linq将在ObservableCollection中搜索产品名称:

private void productSearch_KeyUp(object sender, KeyEventArgs e)
{
      // Search text
      string productSearchText = productSearch.Text.Trim(); 

      // Search product, return no result
      var search = ProductList.Where(s => s.Name.StartsWith(ProductSearchText));

      search.ToList().ForEach(ProductList.Add);
}

Edited: 编辑:

I notice it is because of letter case problem. 我注意到这是因为信件问题。 I needs to be in uppercase, if the product name is in uppercase, how can I search regarding of letter case? 我需要大写,如果产品名称是大写的,我怎么搜索字母大小写?

if the problem is in the case sensetivity you may try to use this 如果问题出在感觉上,你可以尝试使用它

s.Name.ToUppper().StartsWith(ProductSearchText));

or you can try this as well 或者你也可以试试这个

s.Name.ToUppper().StartsWith(ProductSearchText, StringComparison.OrdinalIgnoreCase));

and you probable want to use search foreach like this 你可能想要使用这样的搜索foreach

foreach(var item in search)
{

    // get a search item and do what you want with it 

}

To perform case-insensitive search use another overload of this method: 要执行不区分大小写的搜索,请使用此方法的另一个重载:

var search = ProductList.Where(s => s.Name.StartsWith   
          (ProductSearchText,StringComparison.CurrentCultureIgnoreCase)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM