ef6 icon indicating copy to clipboard operation
ef6 copied to clipboard

Why .OfType<SomeType>() produces a faster query than "x => x is SomeType"?

Open hyankov opened this issue 9 years ago • 1 comments

The following code generates very inefficient query

var result = someIQueryable.FirstOrDefault(op => op is SomeAbstractBaseType && (op as SomeAbstractBaseType).MyProp == myValue);

The following code performs many times better var result = someIQueryable.OfType<SomeAbstractBaseType>().FirstOrDefault(op => op.MyProp== myValue);

The first snippet produces one more (duplicate) SQL fragment of the kind: CASE WHEN ([Extent1].[Discriminator] = N'SomeType...') THEN '0X0X0X0X0X' WHEN

hyankov avatar Nov 28 '17 16:11 hyankov

@hyankov Because the EF6 query translator treats these things differently, probably because the former is a more general type of expression that could be composed on to produce different kinds of results. This is not something we currently have any plans to change.

ajcvickers avatar Dec 04 '17 21:12 ajcvickers