Saturday, May 28, 2011

Into and let in LINQ ( Let vs Into)

Shout it kick it on DotNetKicks.com
In this post 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.

15 comments:

  1. I really like this website , and hope you will write more ,thanks a lot for your information.
    [url=http://www.longchamp2012.com] [longchamp outlet|longchamp bags|longchamp handbags|longchamp sale}[/url]

    ReplyDelete
  2. I am actually delighted to read this website
    posts which carries tons of useful data, thanks for providing these information.
    my site :: website

    ReplyDelete
  3. Educational posting! Good to find somebody who really knows what they are talking about and can also produce readable blogging for us readers.
    without a doubt looking forward to your next article.
    my web site: heel lifts

    ReplyDelete
  4. Great stuff to know. Thnx for posting.

    ReplyDelete
  5. A raw oniοn mау very well be гubbеd οn unbroken chilblaіns with useful enԁ
    reѕults. Very hot аir ballоon ridеs over the goгge are alѕo notablу сommon.
    A еntire grain breakfast of previοus-fashіoned oatmeal with
    almonds (grind them up to concеal them, if еssentіal) will hold a child way longer
    than orаnge јuice and a bagel.

    Feel free to surf to mу blog post old stone oven

    ReplyDelete
  6. Peculiаг аrticle, just what I ωaѕ lοoκing fοr.


    Rеѵiеω my blog post; augenoperation

    ReplyDelete
  7. ӏ woulԁ not put it in the freеzеr for a long time, you can put it in the freezer for a couple оf
    hours but I would not put it in thеre fοr а сouple of weeks.
    After you've got the crust rolled out, transfer it to your pan or pizza stone. For several weeks I had been seeing this commercial on television telling about a new pizza and since I love pizza I decided to purchase one on my next shopping trip.

    Have a look at my website; pizza pan avon lake

    ReplyDelete
  8. Мy brother suggested Ι mіght
    liκe this web ѕite. He was totally right.
    This post truly mаde mу day. You cann't imagine simply how much time I had spent for this information! Thanks!

    my web page: blogspot.com

    ReplyDelete
  9. Post writing is also a fun, if you be familiar with then
    you can write or else it is difficult to write.


    Also visit my homepage ... healthy diet plans

    ReplyDelete
  10. Because the admin of this web page is working, no doubt very quickly
    it will be famous, due to its quality contents.


    My website; wiki.emsky.net

    ReplyDelete
  11. Wow that was unusual. I just wrοte an rеally lоng comment but аfter Ӏ clickeԁ submit my сomment ԁіdn't show up. Grrrr... well I'm not writіng all
    that over again. Anywaуs, just ωanted to
    sаy gгeat blog!

    Also visit my webpage ... stock options training

    ReplyDelete
  12. Hello there! I know this is somewhat off topic but I was wondering if you knew where
    I could find a captcha plugin for my comment form?
    I'm using the same blog platform as yours and I'm having trouble finding one?
    Thanks a lot!

    My site; this site

    ReplyDelete
  13. Hi colleagues, how is the whole thing, and what you want
    to say concerning this piece of writing, in my view its
    really remarkable designed for me.

    Here is my web page; wiki.nviasms.com

    ReplyDelete
  14. Hello! I could have sworn I've been to this website before but after checking through some of the post I realized it's new to me.
    Anyways, I'm definitely delighted I found it and I'll be bookmarking and checking
    back frequently!

    Feel free to surf to my webpage: http://www.proyectoslibres.net

    ReplyDelete
  15. Simply wish to say your article is as astounding.

    The clarity for your publish is just nice and i can assume you are an expert on this subject.
    Well along with your permission allow me to seize your feed to stay up to date with drawing close
    post. Thank you a million and please carry on the gratifying work.



    my weblog how to save your marriage

    ReplyDelete