开发者

Filter BindingSource with entity framework

开发者 https://www.devze.com 2023-02-07 17:54 出处:网络
Hi How can i filter results exists in BindingSource filled with entities ( using EF 4)? I tried this: mybindingsource.Filter = \"cityID = 1\"

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 r开发者_JAVA百科esults.

Thanx


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.


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);
    }


.where (Function (c) c.cityID = 1)
0

精彩评论

暂无评论...
验证码 换一张
取 消