Linq join over 2 datacontexts

C

C.

Hi,

I require two datacontexts to manage users, one to manage details, and
the other for authentication. I am attempting to build a list of User
business objects, but I keep getting the "The query contains
references to items defined on a different data context" exception.

I have fiddled with GetUserDetails() to return IQueryable or
IEnumerable, but neither solution works. Anyone know how I can get the
following to work?

public static List<BO.User> GetUsers(this Table<User> source, int
groupID)
{
ACEDataContext dc = (ACEDataContext)source.Context;

var UserDetails = GetUserDetails();

var users = (from user in dc.Users
where user.GroupId == groupID
join details in UserDetails on user.username
equals details.username
select new BO.User
{
ID = user.id,
Username = user.username,
AdminID = user.groupId,
AccessLevel = user.accessLevel,
EmailAddress = details.emailAddress,
FirstName = details.firstName,
LastName = details.lastName,
}).ToList();

return users;
}

/// <summary>
/// A private method that lets us build our AdminGroupUser
object
/// over DataContext boundaries
/// </summary>
/// <returns></returns>
private static IEnumerable<UserDetail> GetUserDetails()
{
using (UserDetailsDataContext userDC = new
UserDetailsDataContext())
{
var details = from userDetail in userDC.UserDetails
select userDetail;
return details.AsQueryable();
}

}
 
G

Gregory A. Beamer

C. said:
Hi,

I require two datacontexts to manage users, one to manage details, and
the other for authentication. I am attempting to build a list of User
business objects, but I keep getting the "The query contains
references to items defined on a different data context" exception.

In LINQ to SQL, you are stuck. If you want to get results first and then
iterate over two related collections, you can get past the error, but if the
idea behind the query is to filter the results so you don't hammer the
server, you are lost with this idea.

Why not a combined context for this type of work? I am assuming both
contexts hit the same database? PUt it in another namespace to avoid clashes
with your other work. The other option is get the full set from each and
then fold together, but on a large domain, it WILL NOT scale.

As an aside, I am not fond of LINQ to SQL. I find LINQ to be a lovely way of
filtering object sets, etc, but the idea of LINQ as both a means of working
with IEnumerable and a data access technology blurs the separation of
concerns and makes for a messy data layer. EF is better, but I am holding
out for EF 4.0 where my objects can be objects.

--
Peace and Grace,
Greg

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

************************************************
| Think outside the box! |
************************************************
 
C

C.

hrm...unfortunately the authentication is performed on a remote object
for security reasons, and user state is saved locally. I think I'll
just write two helper methods that return Lists from the two
datacontexts. The returned Lists will be fairly small, so shouldn't be
an issue.

Regards,

Chris
 
G

Gregory A. Beamer

C. said:
hrm...unfortunately the authentication is performed on a remote object
for security reasons, and user state is saved locally. I think I'll
just write two helper methods that return Lists from the two
datacontexts. The returned Lists will be fairly small, so shouldn't be
an issue.

You still might have the ability to use LINQ over the two sets of returned
objects, if you desire, but LINQ to SQL is out. Good luck!

--
Peace and Grace,
Greg

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

************************************************
| Think outside the box! |
************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top