简体   繁体   中英

ObjectQuery<T> or IQueryable<T>

I Use ObjectQuery as datasource for a couple of BindingSources in my winforms app. Problem is that Im used to Linq like queries so I use them defining datasource:

View.DsMyDataSource = (from p in ModelContext.GetContext.MyObject 
                       where p.Deleted == false  p) as ObjectQuery<MyObject>;

ModelContext.GetContext() returns singleton of my modelContext entity.

Is it good way of doing that ? I'm afraid that using LINQ like queries I may loose something because of cast.

Is there any other way I can get ObjectQuery type using linq syntax ?

Thanks for any hint.

Well actually ObjectQuery<T> implements IQueryable<T> , so there is no real difference.

I would advise to not bind to an IQueryable, as this is giving too much power to the UI. The point of IQueryable is to defer execution of queries to a later point in time (such as a BLL, services layer), but presentation IMO is too late.

When i say it is too late, i mean that by the time the query gets to the UI, no more queries against the database should be made. But if the UI starts performing operations such as .Count() or .Sum() , you have 2 queries executed. Makes it very difficult to dispose of your data context.

My advice, return a concrete collection: eg ICollection<T> , and bind to that.

If you need to do things like paging, then do that via LINQ .Skip() and .Take() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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