开发者

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: e.g ICollection<T>, and bind to that.

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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜