-1

I often use LinQ statements to query with EF, or to filter data, or to search my data collections, but I've always had that doubt about which is the first statement to write.

Let's say we have a query similar to this:

var result = Data.Where(x => x.Text.StartsWith("ABC")).OrderBy(x => x.Id).Select(x => x.Text).Take(5).ToList();

The same query works even if the statements are in different order, for example:

var result = Data.OrderBy(x => x.Id).Select(x => x.Text).Where(x => x.Text.StartsWith("ABC")).Take(5).ToList();

I understand that there are certain statements that do modify the expected result, but my doubt is with those that do not modify, as in the previous example. Does a specified order or any good practice guide exist for this?

Richard
  • 568
  • 1
  • 6
  • 23
  • 1
    The supposed "duplicate" is about performance, this question neither mentions or implies that it is performance related. – jmoreno Oct 25 '17 at 01:36
  • 1
    Unless `x.Text` also has `Id` and `Text` properties, neither query would work. – D Stanley Oct 25 '17 at 14:16
  • For EF, if the queries are *logically equivalent* and translated well into SQL, and the optimizer is doing its job well, they *should* result in identical query plans. – Damien_The_Unbeliever Oct 25 '17 at 14:24
  • Ignore the example a little, the central question is whether or not there is an order or a guide to good practices for the use of sentences and why – Richard Oct 25 '17 at 14:33

2 Answers2

2

It will give you different results. Let's assume that you have following ids:

6,5,4,3,2,1

The first statement will give you

 1,2,3,4,5

and the second one

2,3,4,5,6

I assumed that all objects with following ids start with ABC

Edit: I think I haven't answered the question properly. Yes, there is a difference. In the first example you only sort 5 elements however in the second one you order all elements which is definitely slower than the first one.

MistyK
  • 6,055
  • 2
  • 42
  • 76
  • You're right, I think the example does not help to show the purpose of the question. I will modify it. – Richard Oct 25 '17 at 14:12
1

Does a specified order or any good practice guide exist for this?

No, because the order determines what the result is. In SQL (a declarative language), SELECT always comes before WHERE, which comes before GROUP BY, etc., and the parsing engine turns that into an execution plan which will execute in whatever order the optimizer thinks is best.

So selecting, then ordering, then grouping all happens on the data specified by the FROM clause(s), so order does not matter.

C# (within methods) is a procedural language, meaning that statements will be executed in the exact order that you provide them.

When you select, then order, the ordering applies to the selection, meaning that if you select a subset of fields (or project to different fields), the ordering applies to the projection. If you order, then select, the ordering applies to the original data, then the projection applies to the ordered data data.

In your second edited example, the query seems to be broken because you are specifying properties that would be lost from the projection:

var result = Data.OrderBy(x => x.Id).Select(x => x.Text).Where(x => x.Text.StartsWith("ABC")).Take(5).ToList();
                                                        ^        

at this (^) point, you are projecting just the Text property, which I'm assuming sia string, and thus the subsequent Select is working on a collection of strings, which would not have a Text property to filter off of.

Certainly you could change the Where to filter the strings directly, but it illustrates that shifting the order of commands can have a catastrophic impact on the query. It might not make a difference, as you are trying to illustrate, for example, ordering then filtering should be logically equivalent to filtering then ordering (assuming that one doesn't impact the other), and there's no "best practice" to say which should go first, so the right answer (if there is one) would be determined on a case-by-case basis.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • This was the answer I was waiting for and with your examples well explained I can give me more idea when making my sentences. Thank you very much friend – Richard Oct 25 '17 at 14:43