RIA Services和MVVM加载,一个查询数据的问题(分离数据)

发布于 2024-09-16 20:46:19 字数 1572 浏览 1 评论 0原文

首先,抱歉标题不好,我只能描述问题

假设服务器上的数据库有一个名为“任务”的表/类型 并且这些任务可以由用户拥有并分配给用户。

SomeTask.Owner = SomeUser
SomeTask.Assignee = SomeOtherUser

在服务器中定义了一些附加查询:

public IQueryable<Task> GetAssignedTasks(int UserId) { /* gets the assigned tasks */ };
public IQueryable<Task> GetOwnedTasks(int UserId) { /* gets the owned tasks */ };

在 ViewModel 中,可以这样加载这些查询:

var ownedTasksQuery = context.GetOwnedTasksQuery(userId);
context.Load(ownedTasksQuery);

var assignedTasksQuery = context.GetAssignedTasksQuery(userId);
context.Load(assignedTasksQuery);

这里的问题是两个结果都加载到上下文中, 即 context.Tasks 包含两个查询结果的并集

我在这里的第一个想法是简单地更改 VieWModel 中属性的 getter:

public IEnumerable<Task> OwnedTasks
{
    get { return context.Tasks.Where(t => t.UserId == userId); }    
}

public IEnumerable<Task> AssignedTasks
{
    get { return context.Tasks.Where(t => t.UserId == userId); }
}

但是,当我将视图绑定到这些属性时,不会返回任何内容, 而如果我在哪里使用以下内容,则返回所有加载的记录(显然):

public IEnumerable<Task> OwnedTasks
{
    get { return context.Tasks; }    
}

public IEnumerable<Task> AssignedTasks
{
    get { return context.Tasks; }
}

我猜我正在以完全错误的方式处理这个问题, 处理这种情况的正确方法是什么?

更新:或者我应该通过创建上下文的另一个实例来简单地处理这个问题?

更新:看来我的做法是错误的...... 我仍在考虑对数据库查询进行分类...

为了解决我的问题,我所要做的就是加载用户,包括分配的和拥有的任务...

ObjectContext.Users.Include("OwnedTasks").Include("AssignedTasks")

将创建这个社区维基,以防其他人做同样的事情事物。

First of all, sorry for the bad title, I can only describe the problem

Let's say the database on the server has a table/type called Tasks
and these tasks can be owned by a user and assigned to a user.

SomeTask.Owner = SomeUser
SomeTask.Assignee = SomeOtherUser

In the server some additional queries are defined:

public IQueryable<Task> GetAssignedTasks(int UserId) { /* gets the assigned tasks */ };
public IQueryable<Task> GetOwnedTasks(int UserId) { /* gets the owned tasks */ };

In the ViewModel these could be loaded as such:

var ownedTasksQuery = context.GetOwnedTasksQuery(userId);
context.Load(ownedTasksQuery);

var assignedTasksQuery = context.GetAssignedTasksQuery(userId);
context.Load(assignedTasksQuery);

The problem here is that both results get loaded into the context,
ie, context.Tasks contains the union of both query results

My first thought here was to simply change the getter for the properties in my VieWModel:

public IEnumerable<Task> OwnedTasks
{
    get { return context.Tasks.Where(t => t.UserId == userId); }    
}

public IEnumerable<Task> AssignedTasks
{
    get { return context.Tasks.Where(t => t.UserId == userId); }
}

However, when I bind the view to these properties, nothing is returned,
whereas if I where to use the following, all loaded records are returned (obviously):

public IEnumerable<Task> OwnedTasks
{
    get { return context.Tasks; }    
}

public IEnumerable<Task> AssignedTasks
{
    get { return context.Tasks; }
}

I'm guessing I'm going about this completely the wrong way,
what's the correct way to handle a situation like this?

Update: Or should I simply handle this by creating another instance of the context?

Update: Seems I was going at this the wrong way...
I'm still thinking in terms of classing database queries...

All I have to do to solve my problem here is load the User including the assigned and owned tasks...

ObjectContext.Users.Include("OwnedTasks").Include("AssignedTasks")

Will make this community wiki, in case somebody else does the same thing.

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

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

发布评论

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

评论(1

剪不断理还乱 2024-09-23 20:46:19

您的意思是放置 Where(t => t.UserId = userId) 还是:

Where(t => t.UserId == userId);

即您使用了赋值运算符而不是比较。 (LINQ 在幕后将 SQL 中的“==”转换为“=”,但必须使用 C# 运算符)。

Did you mean to put Where(t => t.UserId = userId) or did you mean:

Where(t => t.UserId == userId);

i.e. you used an assignment operator instead of compare. (LINQ converts "==" to "=" in SQL behind the scenes, but you must use C# operators).

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