1

I have this LinQ:

var IPI = item.INV_TAXES.Where(t => t.TAXTYPES.TAXNAME == "IPI")
                        .Select(t => new {TOT_AMT = t.TAXVALUE, t.TAXFACTOR, t.TAXBASE})
                        .First();

Then after in the code I call the next lines about 10 times:

PerformSomeCalculation(IPI.TOT_AMT);
PerformAnotherStuff(IPI.TOT_AMT,IPI.TAXVALUE);
PerformSomethingElse(IPI.TAXBASE);

I wonder if everytime that I call each member of IPI, the LinQ executes or just the first time when I assign it?

Is it better to assign the IPI members to a variable first?

decimal IPI_TOT_AMT = IPI.TOT_AMT,
        IPI_TAXVALUE = IPI.TAXVALUE,
        IPI_TAXBASE = IPI.TAXBASE;

And then use them.

Thanks for all the advises.

5 Answers 5

7

First off, STOP SHOUTING IN YOUR CODE.

You are right to be concerned. A query is executed fresh every time you access it, because the results might change. But the result of First is not a query, it is a value.

That is, if you did this:

var query = whatever.Where(whatever).Select(whatever);
Console.WriteLine(query.First());
Console.WriteLine(query.First());

Then the query is created by the first line, executed by the second line, and executed again by the third line. The query does not know whether the first result is different the second time you call First, so it runs the query again.

By contrast, if you do this:

var query = whatever.Where(whatever).Select(whatever);
var first = query.First();
Console.WriteLine(first);
Console.WriteLine(first);

Then the first line creates the query, the second line executes the query and stores the result, and the third and fourth lines report the stored result.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't like to shout in the code, but it's a silly requirement to have those properties and variables in capital letters, sorry about that.
4

In the code you provided, the LINQ query will only run when you call the .First() method.

There will be no noticeable performance improvement by assigning the members to variables before accessing them.

Note that this is not always the case with all LINQ statements. For example, if you had said:

var IPIs = item.INV_TAXES.Where(t => t.TAXTYPES.TAXNAME == "IPI")
                     .Select(t => new {TOT_AMT = t.TAXVALUE, t.TAXFACTOR, t.TAXBASE});

... and then passed IPIs into several methods, you would likely end up with separate database round-trips. In order to avoid that issue, you would call .ToList() to force immediate evaluation before assigning the variable. But in this case calling .First() effectively does the same thing.

Comments

1

I wounder if everytime that I call each member of IPI, the LinQ executes everytime or just the first time when I assign it?

No, the query is only performed once - IPI is an instance of an anonymous type with a bunch of primitive properties (decimals in fact). This object is not connected to a query anymore - your query was executed and returned this object as a result of the First() extension method which forces immediate execution and returns the first item in the input collection.

Comments

1

The answer by Eric Lippert explains all that you wanted. However, if you want to go further in the optimalization, you can try using CompiledQuery.Compile method to store and reuse the queries.

For more information, check msdn. You can start here: http://msdn.microsoft.com/cs-cz/library/bb399335.aspx

Comments

0

The First() method execute the linq query and return the first object This object is typed like any other object you use, the only difference is that the class definition is written by the compiler.

So nothing is executed any more when you use the object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.