Carvellis
Carvellis

Reputation: 4042

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

Assume the following table MyObjects:

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

I have put an additional index on DecimalField.

Consider the following LINQ to Entities query to retrieve an object:

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

Because of the index, it is important that the EF-generated query keeps the order of the properties in the WHERE clause the same (i.e. DecimalField first and TextField second), otherwise a table scan will occur and the index is useless. How can I enforce EF to keep a certain order in the WHERE clause? And is there a difference between ad hoc and compiled queries?

Upvotes: 4

Views: 1356

Answers (1)

KristoferA
KristoferA

Reputation: 12397

No, the order of where clause predicates does not matter. SQL Server will use the appropriate index if there is one, even if your where clause specifies the columns in a different order than they appear in the index.

Upvotes: 6

Related Questions