RIA 服务 - 自定义查询
我陷入了某种进退两难的境地。
尝试从我的 silverlight 客户端通过 RIA 服务查询我的数据库时,我遇到了以下问题。目标是获取菜单和子菜单的列表。 (注意:我只有 2 级菜单列表,因此只有 1 个主菜单和多个子菜单,但这些子菜单不/不能包含其他子菜单)。
第一部分:
public void LoadMenus(Action<ObservableCollection<Menu>> callback)
{
_context.Load(_context.GetMenusQuery(), lo =>
{
var menus = new EntityList<Menu>(_context.Menus, lo.Entities);
foreach (var m in menus.Where(x => x.Parent == x.MenuID))
{
foreach (var sm in LoadMenusByParentID(m.MenuID))
{
m.SubMenus.Add(sm);
}
}
callback.Invoke(menus);
}, null);
}
loadMenusByParentID 非工作版本:
private IEnumerable<Menu> LoadMenusByParentID(int parentID)
{
var lo = _context.Load(_context.GetMenusByParentIDQuery(parentID));
var m = new EntityList<Menu>(_context.Menus, lo.Entities);
return m;
}
工作版本:
private IEnumerable<Menu> LoadMenusByParentID(int parentID)
{
return _context.Menus.Where(m => m.Parent == parentID && m.MenuID != parentID);
}
试图放下良好的做法,我打算使用 Load() 函数(也因为它是异步操作)。但由于某种原因,我没有从中得到结果,而当我 LinqQuery 上下文本身时,我确实得到了结果。
我的非工作版本的服务方法如下所示:
public IQueryable<Menu> GetMenusByParentID(int parentID)
{
return ObjectContext.Menus.Where(m => m.Parent == parentID && m.MenuID != parentID);
}
有人知道为什么这该死的东西不工作吗?
非常感谢! 亲切的问候, 汤姆
I'm in some kind of a dilemma.
Trying to query my DB through a RIA service from my silverlight client, I ran into the following problem. The goal is getting a list of menus and submenus. (Note: I only have a 2 level menus list, so only 1 main menu and multiple submenus, but those submenus don't/can't contain other submenus ).
First part:
public void LoadMenus(Action<ObservableCollection<Menu>> callback)
{
_context.Load(_context.GetMenusQuery(), lo =>
{
var menus = new EntityList<Menu>(_context.Menus, lo.Entities);
foreach (var m in menus.Where(x => x.Parent == x.MenuID))
{
foreach (var sm in LoadMenusByParentID(m.MenuID))
{
m.SubMenus.Add(sm);
}
}
callback.Invoke(menus);
}, null);
}
The loadMenusByParentID non working version:
private IEnumerable<Menu> LoadMenusByParentID(int parentID)
{
var lo = _context.Load(_context.GetMenusByParentIDQuery(parentID));
var m = new EntityList<Menu>(_context.Menus, lo.Entities);
return m;
}
working version:
private IEnumerable<Menu> LoadMenusByParentID(int parentID)
{
return _context.Menus.Where(m => m.Parent == parentID && m.MenuID != parentID);
}
Trying to put down good practice, I intend to use the Load() function (also because it's and async operation). But for some reason I'm not getting a result from it, while when I LinqQuery the context itself I do get a result.
My Service method for the non working version looks like this:
public IQueryable<Menu> GetMenusByParentID(int parentID)
{
return ObjectContext.Menus.Where(m => m.Parent == parentID && m.MenuID != parentID);
}
Anybody has any idea why the bloody thing ain't working?
Big thanks in advance!
Kind regards,
Tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的“加载”版本不会(并且不能)等待数据加载。该
Load
方法是异步调用,并且立即返回的值具有空的 Entities 集合。IEnumerable 版本返回一个延迟加载查询,因此它会在使用时加载数据。
在提供具体建议之前,我想了解一下您如何使用对
LoadMenusByParentID
的调用,但基本上您需要重新设计使用者以使用异步数据(或坚持使用延迟加载)Your "load" version does not (and cannot) wait for the data to be loaded. That
Load
method is an async call and the value returned immediately has an empty Entities collection.The IEnumerable version returns a lazy loading query instead, so it loads the data when used.
I would like to see how you are consuming the call to
LoadMenusByParentID
before offering specific advice, but basically you need to rework the consumer to use async data (or stick with lazy loading)