views:

27

answers:

1

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.

A: 

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).

Enough already
No that's a typo ... sorry :p
TimothyP
That's ok. Q. Where is the variable "userId", used in the Linq expressions, defined?
Enough already