Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is linq's let keyword better than its into keyword?

Tags:

c#

linq

I'm currently brushing up on LINQ and am trying to comprehend the difference between the let and using the into keyword. So far the let keyword seems better than the into keyword as far as my understanding goes.

The into keyword essentially allows one to continue a query after a projection. (Just want to state explicitly that I'm not referring to the one for group join.)

Given an array of names it allows one to do the following:

var intoQuery =   from n in names   select Regex.Replace(n, "[aeiou]", "")   into noVowel   where noVowel.Length > 2   select noVowel; 

It takes the result of the select and places it into the noVowel variable which then allows one to introduce additional where, orderby, and select clauses. Once the noVowel variable is created, the n variable is no longer available.

The let keyword, on the other hand, uses temp anonymous types to allow you to reuse more than one variable at a time.

You can do the following:

var letQuery =   from n in names   let noVowel = Regex.Replace(n, "[aeiou]", "")   where noVowel.Length > 2   select noVowel; 

Both the noVowel and n variables are available for use (even though I haven't used it in this case).

While I can see the difference, I can't quite understand why one would want to use the into keyword over the let keyword unless one explicitly wanted to make sure that previous variables were not able to be used in latter parts of the query.

So, is there a good reason why both keywords exist?

like image 999
mezoid Avatar asked Mar 13 '09 09:03

mezoid


People also ask

Where vs let in linq?

The benefit of using "Let" is that it makes the query very readable and using this we can create a variable and assign a calculated value in that variable. Then later part of the query can be used either in a select clause or as a where clause or Order by clause.

What is the use of let keyword in Linq?

The Let keyword allows you to create a range variable and initialized with the result of the query expression and then you are allowed to use that variable with the upcoming clause in the same query.

Does C# have let?

C# let KeywordUse the let keyword in query expressions to declare and assign variables that can be reused. Let. This C# keyword is a part of a query expression. It introduces a variable.


1 Answers

Yes, because they're doing different things, as you've said.

select ... into effectively isolates the whole of one query and lets you use it as the input to a new query. Personally I usually prefer to do this via two variables:

var tmp = from n in names           select Regex.Replace(n, "[aeiou]", "");  var noVowels = from noVowel in tmp                where noVowel.Length > 2                select noVowel; 

(Admittedly in this case I would do it with dot notation in two lines, but ignoring that...)

Often you don't want the whole baggage of the earlier part of the query - which is when you use select ... into or split the query in two as per the above example. Not only does that mean the earlier parts of the query can't be used when they shouldn't be, it simplifies what's going on - and of course it means there's potentially less copying going on at each step.

On the other hand, when you do want to keep the rest of the context, let makes more sense.

like image 57
Jon Skeet Avatar answered Sep 18 '22 18:09

Jon Skeet