开发者

Silverlight WCF RIA load from single item

Hello I'm a little confused as to how I would get a single "Project" from table in a single item instead of a collection...

this is my viewmodel :

public class ProjectViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    private int _loadedProjectId;
    public int LoadedProjectId
    {
        get
        {
            return _loadedProjectId;
        }
        set
        {
            if (value != _loadedProjectId)
            {
                _loadedProjectId = value;
                _Context.Load(_Context.GetProjectFromIdQuery(_loadedProjectId)); 
                PropertyChanged(this, new PropertyChangedEventArgs("LoadedProjectId"));
            }
        }
    }

    public Project loadedProject { get; set; }
    public IEnumerable<Project> Projects { get; set; }

    private myDomainContext _Context = new myDomainContext();

    public ProjectViewModel()
    {

        Projects = _Context.Projects;
        if (!DesignerProperties.IsInDesignTool)
        {
            _Context.Load(_Context.GetProjectsQuery());
        }

    }
}

The problem is here

_Context.Load(_Context.GetProjectFromIdQuery(_loadedProjectId)); 

as I don't want to load it in my context but I would want to do something like this : (but obviously I can't)

loadedProjec开发者_运维知识库t = _Context.GetProjectFromIdQuery(_loadedProjectId);

Here are the queries in my Domain Service :

    public IQueryable<Project> GetProjects()
    {
        return this.ObjectContext.Projects;
    }

    public Project GetProjectFromId(int id)
    {
        return this.ObjectContext.Projects.SingleOrDefault(t => t.Id == id);
    }

So am I doing this the wrong way? Should I just search the project in the collection of Projects I already have?

tx for shedding a light on this!


RIA Services works with collections, also when your query method returns only one single element. To access the single element you have to use the returned LoadOperation<Project> instance of the call

LoadOperation<Project> loadOperation = _Context.Load(_Context.GetProjectFromIdQuery(_loadedProjectId));

The LoadOperation provides a property Entities that contains all entity that are loaded by the operation. So in your case it contains an enumeration with only one entity.

To get the entity from the collection you can do it like that

Project loadedProject = loadOperation.Entities.FirstOrDefault(p=>p.Id=_loadedProjectId);

If you really dont want to add the loaded entity to your context (what i don´t recommened) you have to use an Invoke-Operation, but then you have no change tracking and association management for loaded entity.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜