0

I'd like to be sure that LET is lazy evaluated in LINQ. It means, that even when LINQ query is actually evaluated, LET clauses, which are not used due to circumstances of conditions, are not evaluated. So my question is not about deferred execution of LINQ queries, but about particular part of query.

Obviously, I cat write a query like this and see that LET is lazy evaluated:

Dim dd = From x In {0, 1, 2, 3} Let y = 1 / x, z = If(True, x + 2, y) Select z

But my experience show that sometimes a logic of VB is not constant, so you can see one behavior in test example, and another in real life (e.g. it happens for "and" and "or").

So I'd like to know that for sure. I didn't find any mention of that particular problem in the docs.

Andrey Minakov
  • 545
  • 2
  • 5
  • 19
  • Can you explain how your example is `lazy` as you have mentioned? – Trevor Apr 02 '19 at 12:51
  • [`Let` is implemented with a `Select`](https://stackoverflow.com/a/1092770/2557128) that creates a new class containing the original query class up to that point and adds a field for the `Let`, so it is just as "lazy" as an intermediate `Select`. – NetMage Apr 02 '19 at 18:11
  • Ok, @ Çöđěxěŕ , possibly it's not lazy in usual meaning for functional language, but I just don't know right term in English. It would be great if you could suggest the right term for "calculating only when necessary" in that particular case. – Andrey Minakov Apr 03 '19 at 06:13
  • @NetMage, I think that there is some possibility of "laziness" for Let in addition to "laziness" for Select. I.e. evaluation of Select may be lazy, but as soon as it happens the evaluation of all the parts of LETs expressions may be done eagerly. – Andrey Minakov Apr 03 '19 at 06:17
  • 1
    There is literally no difference between the `Select and `Let` version - the compiler _immediately_ translates `Let` to `Select`. – NetMage Apr 03 '19 at 16:05

0 Answers0