How to use INTO and LET in LINQ to Get Best Performance

No.of Views2953
Bookmarked0 times
Downloads 
Votes0
By  pranay rana   On  11 Jun 2011 21:06:39
Tag : LINQ , T-SQL
In this article, I am going to discuss, how to use INTO and LET in LINQ in order to get best performance of the LINQ Queries and difference between these.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

I am going to show two keyword of the C# 3.0 is very helpful when playing with the set of object collection using LINQ feature.

INTO

Into keyword allows creating a temporary variable to store the results of a group, join or select clause into a new variable.

var em = from e in emp
                      group e by new{ e.DeptId}
                          into gEmp
                          where gEmp.Count() > 1
                       select new { gEmp.Key.DeptId, salary = gEmp.Sum(t => t.Salary) };

In above query after applying into on groping it creates IGroping type gEmp variable, which used to apply next filter.
Note: Into is use full when you want to perform the operation on the groped data.

LET
Let keyword allows storing the result of the query which can be used in subsequent query i.e it creates new variable and initializes it with the result of expression you supply.

var em = from e in emp
                     group e by new { e.Salary, e.Id }
                         into gEmp
                         let avgsal = (gEmp.Sum(t => t.Salary) / gEmp.Count())
                         where gEmp.Key.Salary == avgsal
                         select new { gEmp.Key.Salary, gEmp.Key.Id };

Above query is used to find out the employee(s) having the salary more than avgSalary. Let keyword allows to create new variable avgsal that used in the further operation.

Let vs Into

Most of the people find difficult which one is to use either Let or Into when designing Linq query.

Into – Hides the previous variable when used in query as you see in above example. Which means its hides previous range variable and create temporary range variable which you can use in further operation.

Let – doesn’t hide the previous variable and create new variable. Which means you created new variable and you can also use the previous variable so you can use both in the further operation.

Thank you reading and state tunning to  get more.

 
Sign Up to vote for this article
 
About Author
 
pranay rana
Occupation-CEO
Company-GMind Solusion
Member Type-Expert
Location-India
Joined date-08 Jan 2011
Home Page-http://pranayamr.blogspot.com
Blog Page-http://pranayamr.blogspot.com
Hey, I am Pranay Rana, working as a Senior Software engineer in mid-size company located in ahmedabad. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 4.3 years now. For me def. of programming is : Programming is something that you do once and that get used by multiple for many years You can visit me on my blog - http://pranayamr.blogspot.com/ StackOverFlow - http://stackoverflow.com/users/314488/pranay My CV :- http://careers.stackoverflow.com/pranayamr
 
 
Other popularSectionarticles
    SelectMany is Projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence.In this post I am going to show how you can use SelectMany Extension method to achieve the join between related tables easily without writing long queries.
    Published Date : 18/Jan/2011
    Now a days most of the developers are moving towards new LINQ to SQL they find difficult to write down SQL query in C# to query data using LINQ. LINQ is a query language which get integrated in C# to query data form ObjectCollects, SQL, XML etc.
    Published Date : 08/Jan/2011
    I have already written several article about Linq its a great ORM that we can use in various way. The purpose of this post to demonstrate How we can bind custom entity to stored procedure result with use of Linq-To-SQL.
    Published Date : 05/Aug/2010
    Today one of my friend asked me about simple insert,update and delete example with LINQ-To-SQL but at that time i was not having any simple example which will show the power of LINQ-To-SQL Example
    Published Date : 17/May/2010
    After two decades, the industry has reached a stable point in the evolution of object-oriented (OO) programming technologies
    Published Date : 12/May/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</