1

Is it possible to filter an ObservableCollection by using an Operator LIKE, like in SQL 2014.

Ex SQL : SELECT * FROM Customer WHERE Name LIKE 'Cal%'

I need the same kind of filtering but with ObservableCollection, I know you have the Where from the Linq library, but it looks only for a EXACT string

Thanks

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
Sunny
  • 21
  • 1
  • 1
    Keep in mind that those collection classes can be used for *any* type of elements. While there are standardized methods for determining *exact* correspondence (cf. `IEquatable` and `IComparable` interfaces), there is no such standardized interface for retrieving the "similarity" of two instances of an arbitrary type. Hence, there is no such filtering method, either; you'll have to write your own, based on your custom similarity measurement. – O. R. Mapper Oct 06 '14 at 13:15
  • 2
    `.Where` + `.StartsWith()`. – Jeroen Vannevel Oct 06 '14 at 13:15
  • 1
    possible duplicate of [Filtering an ObservableCollection?](http://stackoverflow.com/questions/5782585/filtering-an-observablecollection) – Edi G. Oct 06 '14 at 13:16
  • 2
    `.Where(str => str.StartsWith("Cal"))` you just need to specify your expression in the `Where` clause – Jonesopolis Oct 06 '14 at 13:16
  • 1
    @Sunny create `ListCollectionView` from your `ObservableCollection` and use [`Filter`](http://msdn.microsoft.com/en-us/library/cc452341(v=vs.110).aspx) and bind to that – dkozl Oct 06 '14 at 13:16
  • Possible duplicate of [LIKE Operator in LINQ](http://stackoverflow.com/questions/5374481/like-operator-in-linq). – Gutblender Oct 06 '14 at 13:25

3 Answers3

2

Why do you say you can't use Where over IObservableCollection ?

As far as I remeber, you can use Where (and other LINQ methods) : customers.Where(x=>x.StartsWith("Cal")); will return you a List If you need back another observable collection, you must rebuild one new with previous result :

var c = customers.Where(x=>x.StartsWith("Cal"));
customers = new ObservableCollection<Customer>(c.ToList());

According to your needs, you can also use the "Filter" property of CollectionViewSource, see Filtering an ObservableCollection? for example on how to use it.

Community
  • 1
  • 1
AFract
  • 8,868
  • 6
  • 48
  • 70
  • IIRC `.Where()` and the other LINQ methods give back `IEnumerable`, you have to `.ToList()` the results to have a List – Alex Oct 06 '14 at 13:22
  • Good point, ObservableCollection constructor takes a List<>, not an IEnumerable<>. Fixed in my answer. – AFract Oct 06 '14 at 13:24
1

Or you can use CollectionView.

IList<Employer> employers;
ICollectionView _employerView;
private string _filterString=string.Empty;

public Window1()    
{
   InitializeComponent();
    employers = GetCustomers();
   _employerView = CollectionViewSource.GetDefaultView(employers);
   _employerView.Filter = EmployerFilter;
   this.Loaded += new RoutedEventHandler(Window1_Loaded);
} 

public bool EmployerFilter(object item)
{
   Employer employer = item as Employer;
   return employer.Name.ToLower().StartsWith(_filterString.ToLower());
}

public string FilterString
{
   get { return _filterString; }
   set{
  _filterString = value; 
   OnPropertyChanged("FilterString");
  _employerView.Refresh();
}  }
Abdurrahman Alp Köken
  • 1,236
  • 1
  • 13
  • 12
  • 2
    Instead of doing `.Name.ToLower().StartsWith(_filterString.ToLower());` you will get much better performance with `.Name.StartsWith(_filterString, StringComparison.OrdinalIgnoreCase)` which will have the same comparison effect. (actually doing `StringComparison.CurrentCultureIgnoreCase` is the exact same effect, but Ordinal is slightly faster if you don't need the specific culture rules) – Scott Chamberlain Oct 06 '14 at 13:24
0

Where only matches exact when you tell it to match exact with .Where(x=>x.Equals("Cal")), you can do the same as your SQL example by switching to StartsWith. .Where(x=>x.StartsWith("Cal")).

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • Thank guys, I wasn't aware that I could do a startwith directly inside the WHERE Thanks a lot for the answers, really helpfull – Sunny Oct 06 '14 at 13:26