开发者

RIA Service - Custom Query

开发者 https://www.devze.com 2023-04-03 04:55 出处:网络
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 on

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


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)

0

精彩评论

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

关注公众号