3

Hi
How can i filter results exists in BindingSource filled with entities ( using EF 4)?
I tried this: mybindingsource.Filter = "cityID = 1"
But it seems that binding source with entity framework doesn't support filtering .. am i right ?,is there another way to filter(search) data in binding source .

PS:
- I'm working on windows application not ASP.NET.
- I'm using list box to show the results.

Thanx

Dabbas
  • 3,112
  • 7
  • 42
  • 75

3 Answers3

0

I think, you have made mistake in syntax. You should write Filter like this:

mybindingsource.Filter = "cityID = '1'"

Another way is to use LINQ expressions.

(About LINQ) Why do you have to call Entety again?

Simple solution:

    public List<object> bindingSource;
    public IEnumerable FiltredSource
    {
        get{ return bindingSource.Where(c => c.cityID==1);
    }
Leonid
  • 1,071
  • 2
  • 15
  • 27
  • I'll try to change the syntax as you suggested,but what about LINQ expressions ? how can i use it on bindingsource without calling the entities again through the context ? – Dabbas Jan 30 '11 at 18:35
  • 1
    it's not working. the filter prop isn't supported when using EF+bindingSource – Dabbas Feb 16 '11 at 08:28
  • Ok. I've added solution in my answer=) – Leonid Feb 16 '11 at 16:12
  • Thanks @Leonid..i tried you solution, I created a list, assigned it to binding source and filtered it. when i try to add new item to the binding source i get this error message: "Item cannot be added to a read-only or fixed-size list." – Dabbas Mar 15 '11 at 10:25
  • I want it to be traceable reslts – Dabbas Mar 15 '11 at 10:49
  • Binding source which uses Entity Framework as the datasource does not supports filtering - see SupportsFiltering property of your binding source object. – Sandman4 Aug 28 '13 at 08:56
  • @Dabbas, maybe this answer will help you:http://stackoverflow.com/questions/1389831/net-bindingsource-filter-syntax-reference – Leonid Sep 27 '13 at 10:37
0
.where (Function (c) c.cityID = 1)
Bibhas Debnath
  • 14,559
  • 17
  • 68
  • 96
  • I think this requires to call the entities again .. I need to filter the results within the binding source, not going back and getting the results from database then using where ... – Dabbas Feb 16 '11 at 08:27
0

Maybe a better one than Leonid:

private BindingSource _bs;
private List<Entity> _list;

_list = context.Entities;
_bs.DataSource = _list;

Now when filtering is required:

_bs.DataSource = _list.Where<Entity>(e => e.cityID == 1).ToList<Entity>;

This way you keep the original list (which is retrieved once from the context) and then use this original list to query against in memory (without going back and forth to the database). This way you can perform all kinds of queries against your original list.

Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59