Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 338873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:28:12+00:00 2026-05-12T10:28:12+00:00

I asked a question here about an Linq error that results from mixing Linq-To-SQL

  • 0

I asked a question here about an Linq error that results from mixing Linq-To-SQL with C# code. In brief, the compiler gets confused and doesn’t realize that you are intending to call a local function on the resultset after it’s come back from the database.

The answer I accepted was to use AsEnumerable() on the result set, thus forcing it to return all columns before doing any further calculations.

My problem now is that in a very similar way, I am now trying to invoke a method of an object that is related to my resultset via a foreign key, e.g.:

var q =
  from c in MyCities.AsEnumerable() // note the AsEnumerable, forcing the query to execute before accessing properties of c
  let cy = c.County
  select new { CityName = c.Name, CountyName = cy.Name, CountyFoo = cy.Foo() };

And guess what – this throws that ol’ exception again. Why? Clearly because we need to go back to the database again in order to load the related County of c. Only this time, I can’t call AsEnumerable(), because c has only one County, not a collection!

Dwat that wascawwy wabbit!

How do I get around this one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T10:28:12+00:00Added an answer on May 12, 2026 at 10:28 am

    One more level of indirection should fix it, though I don’t know how readable this is. Remove CountyFoo = cy.Foo() and CountyName = cy.Name from your result set and replace it with County = cy to give you..

    var q =
      from c in MyCities.AsEnumerable() 
      let cy = c.County
      select new { CityName = c.Name, County = cy };
    

    then add

    var p = q.AsEnumerable().Select(x => 
        new 
        { 
            CityName = x.CityName, 
            CountyName = x.County.Name, 
            CountyFoo = x.County.Foo() 
        });
    

    You can then enumerate over p. This is quite obfuscated, though. Why do you need all of this in line LINQ query?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i've asked the first question about selects here , i thought, that when i
I have asked this question here about a Python command that fetches a URL
I asked a question about signal on windows with the same code,here is another
I asked a question here : When To Use IEquatable And Why about using
i have asked a question about manytomanyfield here which i solved but now a
Yesterday I asked this general question about decimals and their internal precisions. Here is
I've asked questions on here so many times about this ruddy game that I'm
i asked a question here throw 404 error when route is not satisfied and
A while back, I asked a question on here about how to run an
I've asked here a question about Task Killers and widgets stop working ( SO

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.