为什么RIA Services要返回IQueryable它是什么错误,有什么好处
为什么RIA Services要返回IQueryable<T>,它是什么东东,有什么好处?
我总是以为去asp.net dataset/datatable的经验去理解这东西,后来感觉好像完全不是一回事。
Domain Service上的GetX()看起来像是返回所有数据,但实际上好像返回的是EntityQuery<T>这样的东西,而不是数据本身。
再进一步的解释是When GetX gets called, it is not actually executing a query at all. It is just forming an expression tree and returning that as an IQueryable<T> that describes what could be returned from this method.
服务器端只有GetX(),并没有GetX(...)等overload方法。
至少DomainService的GetX()这样的方法不像asp.net三层模式似的返回的是实际的数据集,而是所谓Expression Tree的东西,这里应该已经涉及到LINQ了,然后实际的数据获取是在执行context.Load()才发生。
------解决方案--------------------
汗颜
我总是以为去asp.net dataset/datatable的经验去理解这东西,后来感觉好像完全不是一回事。
Domain Service上的GetX()看起来像是返回所有数据,但实际上好像返回的是EntityQuery<T>这样的东西,而不是数据本身。
// Just sets up what you want done,
// it doesn’t actually make a call.
EntityQuery<T> query = domainContext.GetXQuery(); //对应Domain Service上的GetX()方法。
// Finally, it gets a LoadOperation<T> from the context by calling Load.
// This is the point where a call is actually made to the server...
LoadOperation<T> loadOp = domainContext.Load(query);
再进一步的解释是When GetX gets called, it is not actually executing a query at all. It is just forming an expression tree and returning that as an IQueryable<T> that describes what could be returned from this method.
服务器端只有GetX(),并没有GetX(...)等overload方法。
XDomainContext context = new XDomainContext();
//...
EntityQuery<T> query = context.GetXQuery();
// The expression tree that is sent to the client can be modified before actually executing it.
LoadOperation<T> loadOp = context.Load(query.Where(t=>t.TId == 1));
至少DomainService的GetX()这样的方法不像asp.net三层模式似的返回的是实际的数据集,而是所谓Expression Tree的东西,这里应该已经涉及到LINQ了,然后实际的数据获取是在执行context.Load()才发生。
------解决方案--------------------
汗颜