Silverlight WCF RIA 从单个项目加载

发布于 2024-11-24 03:15:20 字数 1662 浏览 0 评论 0原文

您好,我对如何从单个项目而不是集合中的表中获取单个“项目”有点困惑...

这是我的视图模型:

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());
        }

    }
}

问题就在这里

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

,因为我不想将其加载到我的上下文,但我想做这样的事情:(但显然我不能)

loadedProject = _Context.GetProjectFromIdQuery(_loadedProjectId);

这是我的域服务中的查询:

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

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

那么我这样做的方式是否错误?我应该只在我已有的项目集合中搜索该项目吗?

tx 阐明了这一点!

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)

loadedProject = _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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你再特殊 2024-12-01 03:15:20

RIA 服务适用于集合,当您的查询方法仅返回一个元素时也是如此。要访问单个元素,您必须使用返回的 LoadOperation;调用的实例

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

LoadOperation 提供了一个属性 Entities,其中包含该操作加载的所有实体。因此,在您的情况下,它包含一个只有一个实体的枚举。

要从集合中获取实体,您可以这样做

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

如果您确实不想将加载的实体添加到您的上下文中(我不推荐),您必须使用调用操作,但是您没有更改跟踪以及加载实体的关联管理。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文