Entity Framework 4: How to ensure WHERE clause order in generated query

Suppose the following table is MyObjects :

 Id (PK, int) DecimalField (decimal) TextField (nvarchar) 

I added an extra index on DecimalField .

Consider a LINQ to Entities query to retrieve an object:

 db.MyObjects.FirstOrDefault(r => r.DecimalField == localValue1 && r.TextField == localValue2) 

Due to the index, it is important that the generated EF query DecimalField order of the properties in the WHERE clause the same (i.e. DecimalField first and TextField second), otherwise the table will be scanned and the index will be useless. How can I make EF stick to a certain order in a WHERE clause? Is there a difference between special and compiled queries?

+4
source share
1 answer

No, the order of predicates in a sentence does not matter. SQL Server will use the corresponding index, if any, even if your where clause indicates the columns in a different order than they appear in the index.

+6
source

Source: https://habr.com/ru/post/1336116/


All Articles