我有以下linq查询
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();
这导致以下预期的SQL,这是我想要实现的
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
但是我想返回MyType而不是匿名类型,所以我写了以下内容
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();
我希望生成相同的查询但返回我的类型。相反,我发现它执行一个大规模的递归查询(基本上查询表中的每个记录)如下所示。
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
看起来它正在为每条记录获取所有fk类型并忽略where子句?
任何人都可以解释我在这里做错了什么,为什么两个生成的查询不同?
答案 0 :(得分:0)
我使用LinqPad测试查询,并在Dump()
结果上调用了ToList()
。 Dump()枚举通过db中所有相关记录引起循环枚举的所有属性。对匿名类型的转储只列出所有属性,没有实体导航属性导致枚举。
将来在实体上使用LinqPad的Dump()方法的所有注释。向所有人道歉,浪费时间。男生错误。