Click here to Skip to main content
Click here to Skip to main content

Optimizing linq queries that retrieve a single element

, 14 May 2012
Rate this:
Please Sign up or sign in to vote.
Problem around .first() method in EF and a good solution

Introduction

In this Tip I want to describe about differences between First() method and TryGetObjectByKey() in projects that use EF and linq. and Why we shouldn't use First() method!

How .First() Method Works?

Easiest way to retrieve a single element is to call First() Method in Your LINQ Query, each time it goes to database and read the data from that.

Question is:
What could happen else? isn't this way?
answer is no! because you are using a big technologies name entity Framework!
the EF have a thing name StateManager!
if you want to know what is state manger you have to go deep inside this technology but here I call this A thing that make the world a better place to live!Smile | :)

Every time you call the first() method its doesn't have any work with that thing!

and here is My lovely method: TryGetObjectByKey()

How TryGetObjectByKey() Method Works?

this Method do the same, it means it search for an element by its key but! wait! yes there is a difference! this method goes to state manger before going to database!
TryGetObjectByKey() ask state manager: " is an entity of that type with the given key exists in memory? " . if state manager says " yes!! " the query returns in-memory entity without going to database! and if the answer is "no!" it queries the database and put it in state manager.

Code in C#:

var c = (customer)ctx.TryGetObjectByKey(new EntityKey("myentity.customer" , "Customer_ID" , "ALFKI"));
var c2 = (customer)ctx.TryGetObjectByKey(new EntityKey("myentity.customer" , "Customer_ID" , "ALFKI"));      

code in VB:

Dim c = DirectCast(ctx.TryGetObjectByKey(new EntityKey("myentity.customer" , "Customer_ID" , "ALFKI")),Customer );
Dim c2 = DirectCast(ctx.TryGetObjectByKey(new EntityKey("myentity.customer" , "Customer_ID" ,"ALFKI")),Customer );      

This Method has 3parameter: first is the entity name, second is the key column and the third is its key value!

first query for c go to database, but for c2 use in-memory entity!

But What happen if the data modifed at the same time when query use in-memory entity?

EF have a mechanism call " change Tracker ". but as i say if you want to know more about EF you have to read books and go deep inside it!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

taha bahrami
Web Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
Taha has started programming at the age of 16 and he has taken an avid interest in Microsoft technologies. He professionally works on ASP.NET and C#. Mainly, He lives for getting the world into codes and follows this aspiration in a third world country with lack of facility and support. He never gives up seeking success and competence.
Follow on   Twitter   Google+

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 14 May 2012
Article Copyright 2012 by taha bahrami
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid