0

I currently have a LINQ query implemented as a method of my DataContext class. This method calculates the permissions a user has for a page within a Wiki/CMS. What I'd like to do is relocate this method to the Page and User LINQ data objects.

However, when I move this query to the individual data objects it executes using LINQ to Objects instead of LINQ to SQL. This means that it pulls several full collections from the database server every time it executes.

Here is a fragment of the code that uses LINQ to Objects. I'd like this to use LINQ to SQL:

partial class WikiPage
{
    public void GetUserPermissions(Guid? userId) {
        var usersPlusAnon =
            (
                from user in this.Wiki.WikiWikiUsers
                select new { user.WikiId, WikiUserId = (Guid?)user.WikiUserId }
            ).Union(
                from wiki in new Wiki[]{this.Wiki}
                select new { wiki.WikiId, WikiUserId = (Guid?)null }
            );
    }
}

Here is a fragment of the code that works, using LINQ to SQL. I'd rather have this method on the WikiPage class than the DataContext:

partial class WikiDataContext
{
    public void GetUserPermissions(Guid? userId) {
        var usersPlusAnon =
            (
                from user in this.WikiTomeUsers
                join wikiUser in this.WikiWikiTomeUsers on user.WikiTomeUserId equals wikiUser.WikiTomeUserId into tmp_wikiUser
                from wikiUser in tmp_wikiUser.DefaultIfEmpty()
                select new { WikiId = wikiUser.WikiId, WikiTomeUserId = (Guid?)wikiUser.WikiTomeUserId }
            )
            .Union(
                from wiki in this.Wikis
                select new { WikiId = wiki.WikiId, WikiTomeUserId = (Guid?)null }
            );
    }
}

When the LINQ query is run from the DataContext, it executes as LINQ to SQL, but when run from the WikiPage LINQ object it runs as LINQ to Objects. Is something off with my syntax here, or is this simply not possible with LINQ?

5
  • Your question is a little confusing as to which code is producing which results...can you give us the code that does each (LINQ to SQL vs. LINQ to Objects) seperately to make it a little more clear? Commented Jul 29, 2009 at 23:21
  • @Justin Niessner Done... The code is the same, the only difference is whether it's run from the DataContext class, or a LINQ object. Commented Jul 29, 2009 at 23:26
  • Is this.Wiki the DataContext in the first example? Commented Jul 29, 2009 at 23:28
  • @AaronSieb Thanks for the update. Hopefully my answer makes sense. Commented Jul 29, 2009 at 23:30
  • @csunwold this.Wiki is another LINQ data object (Wiki and WikiPage are related tables) Commented Jul 30, 2009 at 1:20

1 Answer 1

1

The reason you are getting different behaviors from both sets of code is because you are using the 'this' keyword.

When you write your code as part of the data context, it is using the LINQ to SQL classes in the DataContext object (because the 'this' keyword is pointing to the DataContext).

If you want the code outside of the DataContext to use LINQ to SQL, you have to change your reference to 'this' to an instance of your WikiDataContext. It should then use LINQ to SQL to pull your data from the database rather than LINQ to Objects.

Sign up to request clarification or add additional context in comments.

3 Comments

How do I reference the related data context from within a LINQ object?
WikiDataContext context = new WikiDataContext();
Ahh, of course. I was thinking of potential conflicts if the return result is modified and saved, but they don't apply in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.