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 8204953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:03:41+00:00 2026-06-07T08:03:41+00:00

I am trying gain more understanding of Linq queries and Entity Framework (4.1). Please

  • 0

I am trying gain more understanding of Linq queries and Entity Framework (4.1). Please take a look at following two queries. Both queries returns car type name (CarType.Name)

In first query I used join and ignored navigational property CarType

from c in Cars.AsEnumerable().
Where(e => e.CarId == Guid.Parse("0501cc96-5610-465d-bafc-16b30890c224"))
join ct in CarTypes on c.CarTypeId equals ct.CarTypeId
select new CarType {
    Name = ct.Name
}

In second, I used navigational property CarType

from c in Cars.AsEnumerable()
where c.CarId == Guid.Parse("0501cc96-5610-465d-bafc-16b30890c224")
select new CarType {
    Name = c.CarType.Name
}

I ran both in LinqPad therefore there is Guid.Parse function.

When I run these, first statement runs faster. LinqPad reports 00:00:036.
Second statement runs slower and LinqPad reports 00:00:103

Looking at results it seems that Linq queries that use joins instead of navigational properties are faster. Is that really so? Please somebody shead some light to this. Are there any general guidances, the best practices that I should follow when writing Linq queries?

Thanks

  • 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-06-07T08:03:42+00:00Added an answer on June 7, 2026 at 8:03 am

    Since you are calling .AsEnumerable(), the queries are not evaluated using LINQ to Entities, but rather LINQ to Objects.

    This means that the first one is likely doing two round-trips: one to pull all the Cars and one to pull all the CarTypes. Then it performs a join locally, using whatever algorithm LINQ to Objects uses for such operations.

    The second one is probably doing N + 1 round-trips, where N is the number of CarTypes. You do a round-trip to grab all the cars, and then each time one of those cars has a CarTypeId that Entity Framework hasn’t already loaded in, it goes back to the database to select that CarType.

    If you use the SQL tab in LINQPad, you can see all the LINQ queries that are being performed by your program.

    The best practice that you should apply in this case is to not call .AsEnumerable() on an Entity Framework object set. Instead, compose your entire query and then call .ToList() at the end to capture the results. You are probably calling .AsEnumerable() as a workaround because Guid.Parse() doesn’t work inside of a LINQ to Entities query, but you can easily remove that part from the query. In LINQPad, press Ctrl–2 to switch to C# Statement(s) mode, and then run a query like this:

    var guid = Guid.Parse("0501cc96-5610-465d-bafc-16b30890c224");
    var carTypeNames = 
        (from c in Cars
        where c.CarId == guid
        select new CarType {
            Name = c.CarType.Name
        }).ToList();
    carTypeNames.Dump();
    

    The two queries given should have roughly equivalent performance when done right, so you should prefer Navigation Properties, since they are more concise and easier to read. Or, according to your preference, you could turn the query around and make it be based on the CarType collection:

    var guid = Guid.Parse("0501cc96-5610-465d-bafc-16b30890c224");
    var carTypeNames = 
        (from ct in CarTypes
        where ct.Cars.Any(c => c.CarId == guid)
        select new CarType {
            Name = c.CarType.Name
        }).ToList();
    carTypeNames.Dump();
    

    Update

    Avoid creating an entity object like this:

    public class CarTypeSummary
    {
        public string Name{get;set;}
    }
    
    void Main()
    {
        var guid = Guid.Parse("0501cc96-5610-465d-bafc-16b30890c224");
        var carTypeNames = 
            (from ct in CarTypes
            where ct.Cars.Any(c => c.CarId == guid)
            select new CarTypeSummary {
                Name = c.CarType.Name
            }).ToList();
        carTypeNames.Dump();
    }
    

    In production code, it’s often a good idea to decouple your API from the underlying data type, to give you more flexibility to change things without having to modify code anywhere.

    public interface ICarTypeSummary{string Name{get;}}
    public class CarTypeSummary : ICarTypeSummary
    {
        public string Name{get;set;}
    }
    public ICarTypeSummary GetCarTypeSummaryForCar(Guid guid) 
    {
        return (from ct in CarTypes
                where ct.Cars.Any(c => c.CarId == guid)
                select new CarTypeSummary {
                    Name = c.CarType.Name
                }).FirstOrDefault();
    }
    

    This way, if you decide in the future that you would rather just return an actual CarType, to take advantage of Entity Framework’s caching mechanisms, you could change your implementation without messing with the API:

    // Make the Entity class implement the role interface
    public partial class CarType : ICarTypeSummary {}
    
    public ICarTypeSummary GetCarTypeSummaryForCar(Guid guid) 
    {
        return CarTypes.FirstOrDefault(
            ct => ct.Cars.Any(c => c.CarId == guid));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to gain a more thorough understanding of git. Can someone give me
I've been trying to gain a deeper understanding of how compilers generate machine code,
I'm trying to gain a basic understanding of what is meant by a Windows
I'm fairly new to Rails and I'm trying to gain a better understanding of
I am trying to execute some queries using YQL. To gain some efficiency, I
I'm investigating since some days box-shadow and text-shadow. I'm trying to gain the following
I'm trying to gain a better understanding of the plumbing behind ASP.NET and ASP.NET
I've been trying to gain a better understanding of C# through books but I
Two part question Part one: I am trying to create an ASynchronous request to
I'm in the process of trying to 'learn more of' and 'learn lessons from'

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.