I have the following linq query
var result = from myTypes in context.MyTypes
where
((myTypes .Prop1== "Test" ? 1 : 0) +
(myTypes .Prop2 == "Tester2" ? 1 : 0) +
(myTypes .Prop3 == "624642624000000000" ? 1 : 0) +
(myTypes .Prop4 == "TS166TH" ? 1 : 0) +
(myTypes .Prop5 == "1 Test Lane" ? 1 : 0)) >= 4
select new {
myTypes .Prop1,
myTypes .Prop2,
myTypes .Prop3,
myTypes .Prop4,
myTypes .Prop5,
myTypes .OtherProp,
myTypes .OtherTypeId
};
result.ToList();
This results in the following expected SQL which is what I want to achieve
SELECT
[Extent1].[Prop1] AS [Prop1],
[Extent1].[Prop2] AS [Prop2],
[Extent1].[Prop3] AS [Prop3],
[Extent1].[Prop4] AS [Prop4],
[Extent1].[Prop5] AS [Prop5],
[Extent1].[OtherProp] AS [OtherProp],
[Extent1].[OtherTypeId] AS [OtherTypeId],
FROM [dbo].[MyType] AS [Extent1]
WHERE ((CASE WHEN ('Test' = [Extent1].[Prop1]) THEN 1 ELSE 0 END) + (CASE WHEN ('Tester2' = [Extent1].[Prop2]) THEN 1 ELSE 0 END) + (CASE WHEN ('624642624000000000' = [Extent1].[Prop3]) THEN 1 ELSE 0 END) + (CASE WHEN ('TS166TH' = [Extent1].[Prop4]) THEN 1 ELSE 0 END) + (CASE WHEN ('1 Test Lane' = [Extent1].[Prop5]) THEN 1 ELSE 0 END)) >= 4
However I want to return MyType not an anonymous type so I wrote the following
var result = from myTypes in context.MyTypes
where
((myTypes .Prop1== "Test" ? 1 : 0) +
(myTypes .Prop2 == "Tester2" ? 1 : 0) +
(myTypes .Prop3 == "624642624000000000" ? 1 : 0) +
(myTypes .Prop4 == "TS166TH" ? 1 : 0) +
(myTypes .Prop5 == "1 Test Lane" ? 1 : 0)) >= 4
select myTypes;
result.ToList();
Which I would expect to generate the same query but return my type. Instead I find it performs a massive recursive query (essentially querying every record in the table) As below.
SELECT
[Extent1].[Prop1] AS [Prop1],
[Extent1].[Prop2] AS [Prop2],
[Extent1].[Prop3] AS [Prop3],
[Extent1].[Prop4] AS [Prop4],
[Extent1].[Prop5] AS [Prop5],
[Extent1].[OtherProp] AS [OtherProp],
[Extent1].[OtherTypeId] AS [OtherTypeId],
FROM [dbo].[MyType] AS [Extent1]
WHERE ((CASE WHEN ('Test' = [Extent1].[Prop1]) THEN 1 ELSE 0 END) + (CASE WHEN ('Tester2' = [Extent1].[Prop2]) THEN 1 ELSE 0 END) + (CASE WHEN ('624642624000000000' = [Extent1].[Prop3]) THEN 1 ELSE 0 END) + (CASE WHEN ('TS166TH' = [Extent1].[Prop4]) THEN 1 ELSE 0 END) + (CASE WHEN ('1 Test Lane' = [Extent1].[Prop5]) THEN 1 ELSE 0 END)) >= 4
GO
SELECT
[Extent1].[OtherTypeId] AS [OtherTypeId],
[Extent1].[OtherTypeProp] AS [OtherTypeProp],
FROM [dbo].[OtherTypes] AS [Extent1]
GO
-- Region Parameters
DECLARE EntityKeyValue1 BigInt = 1
-- EndRegion
SELECT
[Extent1].[Prop1] AS [Prop1],
[Extent1].[Prop3] AS [Prop2],
[Extent1].[Prop3] AS [Prop3],
[Extent1].[Prop4] AS [Prop4],
[Extent1].[Prop5] AS [Prop5],
[Extent1].[OtherProp] AS [OtherProp],
WHERE [Extent1].[OtherTypeId] = @EntityKeyValue1
GO
-- Region Parameters
DECLARE EntityKeyValue1 BigInt = 2
-- EndRegion
SELECT
[Extent1].[Prop1] AS [Prop1],
[Extent1].[Prop3] AS [Prop2],
[Extent1].[Prop3] AS [Prop3],
[Extent1].[Prop4] AS [Prop4],
[Extent1].[Prop5] AS [Prop5],
[Extent1].[OtherProp] AS [OtherProp],
WHERE [Extent1].[OtherTypeId] = @EntityKeyValue1
GO
It looks like It’s getting all the fk types for EVERY record and ignoring the where clause?
Can anyone explain what I’m doing wrong here and why are the two generated query’s different?
OK That was embarrassing.
I was using LinqPad to test the query and was calling a
Dump()on theToList()result. Dump() enumerates all the properties which caused a cyclic enumeration through all related records in the db. Where as a Dump on the anonymous types just list all the properties and no entity Navigation properties cause enumeration.A note to all using LinqPad’s Dump() method on Entity’s in future. Apologies to all for wasting time. School boy error.