Sunil
Sunil

Reputation: 21396

Records being loaded twice in Entity Framework or only once

I am new to Entity Framework and was looking for some help.

I have the following C# code for my entity data model. My question is: Will payables.Vendors get vendors from the database twice or only once? These statements actually appear exactly in the same order in my code. I am not sure if the first call to payables.Vendors will be cached so the second call to payables.Vendors does not go to the database.

PayablesEntities payables = new PayablesEntities();

var selectedVendor =(from vendor in payables.Vendors
                     where vendor.VendorID == vendorID
                     select vendor).First();

var pendingVendor = (from vendor in payables.Vendors
                     where vendor.IsPending == true
                     select vendor).First(); 

Upvotes: 0

Views: 262

Answers (1)

Selman Genç
Selman Genç

Reputation: 101681

Since you have two separate queries then there will be two SQL queries to execute.LINQ to Entities doesn't magically combine your queries.If you want to execute one query then you need to create one query and combine them.

Upvotes: 1

Related Questions