Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the order of OrderBy, Select and Where clauses in Linq-to-entities matter

Suppose I have a table of students with tons of columns. I want to the EF equivalent of

SELECT id,lastname,firstname 
FROM students 
WHERE coursename='Eurasian Nomads'
ORDER BY lastname,firstname

I just want a subset of the full Student model so I made a view model

public class StudentView{
    public int ID{get;set;}
    public string LastName{get;set;}
    public string FirstName{get;set;}
}

and this EF code seems to work:

List<StudentView> students=context.Students
.Where(s=>s.CourseName=="Eurasian Nomads")
.OrderBy(s=>s.LastName)
.ThenBy(s=>s.FirstName)
.Select(s=>new StudentView(){ID=s.ID,LastName=s.LastName,FirstName=s.FirstName})
.ToList();

But my question is does the order of these clauses matter at all, and if so, what sort of rules should I follow for best performance?

For example, this also seems to work:

List<StudentView> students=context.Students
.Select(s=>new StudentView(){ID=s.ID,LastName=s.LastName,FirstName=s.FirstName})
.OrderBy(s=>s.LastName)
.ThenBy(s=>s.FirstName)
.Where(s=>s.CourseName=="Eurasian Nomads")
.ToList();
like image 461
Matthew Avatar asked Sep 08 '16 15:09

Matthew


People also ask

Does LINQ order matter?

Yes. But exactly what that performance difference is depends on how the underlying expression tree is evaluated by the LINQ provider. For instance, your query may well execute faster the second time (with the WHERE clause first) for LINQ-to-XML, but faster the first time for LINQ-to-SQL.

Is LINQ OrderBy ascending?

In LINQ, the OrderBy operator is used to sort the list/ collection values in ascending order. In LINQ, if we use order by the operator by default, it will sort the list of values in ascending order. We don't need to add any ascending condition in the query statement.

Why from comes first in LINQ?

The reason is, LINQ is used with C# or other programming languages, which requires all the variables to be declared first. From clause of LINQ query just defines the range or conditions to select records. So that's why from clause must appear before Select in LINQ.

Can we use multiple where clause in LINQ?

Well, you can just put multiple "where" clauses in directly, but I don't think you want to. Multiple "where" clauses ends up with a more restrictive filter - I think you want a less restrictive one.

What is LINQ orderby method?

What is Linq OrderBy Method? The Linq OrderBy method in C# is used to sort the data in ascending order. The most important point that you need to keep in mind is this method is not going to change the data rather it is just changing the order of the data.

What are sorting operators in LINQ?

In LINQ, sorting operators are used to rearrange the given sequence in ascending or descending order based on one or more attributes. There are 5 different types of sorting operators are available in LINQ:

What is the use of orderby operator in SQL?

OrderBy operator is used to rearranging the elements of the given sequence in ascending order. This operator by default converts the order of the given sequence in ascending order. There is no need to add an extra ascending condition in the query expression means ascending keyword is optional.

What is the Order of execution of SQL query?

In SQL, the first clause that is processed is the FROM clause, while the SELECT clause, which appears first in an SQL query, is processed much later. The phases involved in the logical processing of an SQL query are as follows: In practice this order of execution is most likely unchanged from above.


1 Answers

The order in which you create your query before it's executed against the server is not relevant in most cases.

Actually one of the advantages is to be able of gradually create the query by concatenating where, order by, and other clauses.

But there are sometimes where the order can affect the generated sql.

Take the samples you provided. They both compile correctly, but the second does not actually get executed. If you try to run this query against an EF database you will get an NotSupportedException:

System.NotSupportedException: The specified type member 'CourseName' is not supported in LINQ to Entities.

The key here is that you are trying to filter the query by the CourseName property in the view model (StudentView) and not the property of the entity. So you get this error.

In the case of the first query, it correctly generates this sql:

SELECT
    [Extent1].[ID] AS [ID],
    [Extent1].[LastName] AS [LastName],
    [Extent1].[FirstName] AS [FirstName]
    FROM [dbo].[Students] AS [Extent1]
    WHERE N'Eurasian Nomads' = [Extent1].[CourseName]
    ORDER BY [Extent1].[LastName] ASC, [Extent1].[FirstName] ASC

So, as you can see the order is critical sometimes.

like image 97
Karel Tamayo Avatar answered Oct 19 '22 21:10

Karel Tamayo