BindingSource Filter with Entity Infrastructure

Hi
How can I filter the results in a BindingSource populated with entities (using EF 4)?
I tried this: mybindingsource.Filter = "cityID = 1"
But it seems that binding the source to the Framework entity does not support filtering. Am I right ?, Is there any other way to filter (search) data in a binding source.

PS:
- I am working on a Windows application, not ASP.NET.
- I use the list box to show the results.

Thanx

+3
source share
3 answers

I think you made a mistake in the syntax. You should write Filter as follows:

mybindingsource.Filter = "cityID = '1'"

Another way is to use LINQ expressions.

( LINQ) Entety?

:

    public List<object> bindingSource;
    public IEnumerable FiltredSource
    {
        get{ return bindingSource.Where(c => c.cityID==1);
    }
0
.where (Function (c) c.cityID = 1)
0

Maybe better than Leonid:

private BindingSource _bs;
private List<Entity> _list;

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

Now that filtering is required:

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

This way you save the original list (which is retrieved once from the context), and then use this original list for the query in memory (without going to the database). Thus, you can fulfill all kinds of queries according to your initial list.

0
source

Source: https://habr.com/ru/post/1788782/


All Articles