linq到实体表达式,包括字典查找

时间:2015-10-13 09:57:43

标签: c# entity-framework linq dictionary linq-to-entities

我想在linq中使用值查找这样的实体:

x => x.Prop1 * values[x.Prop2]

获取错误,因为它无法将其转换为商店表达式。我想我可以在此之前将值写入db并使用外键查找,但这是解决这个问题的唯一方法吗?

(在linq对象中有效地执行此操作的行太多)

更新: 我添加了一个表来从DB端进行查找,使用linq查询,现在看起来像这样:

var result = anEntityRepository.GroupBySum(
x => x.Prop1 * x.NavigationProperty.Prop2,
           g => new { groupedProperty = g.Prop3 }
           );

但是产生的linq不必要地复杂:

SELECT 
[Distinct1].[Prop3] AS [Prop3], 
[Distinct1].[C1] AS [C1], 
(SELECT 
    SUM([Filter4].[A1]) AS [A1]
    FROM ( SELECT 
         [Filter3].[Prop1] * [Filter3].[Prop2] AS [A1]
        FROM ( SELECT [Extent4].[Prop3] AS [Prop3], [Extent4].[Prop1] AS [Prop1], [Extent6].[Prop2] AS [Prop2]
            FROM   [dbo].[anEntity] AS [Extent4]
            LEFT OUTER JOIN [dbo].[NavigationProperty] AS [Extent6] ON [Extent4].[NavigationPropertyId] = [Extent6].[NavigationPropertyId])  AS [Filter3]
        WHERE [Distinct1].[Prop3] = [Filter3].[Prop3]
    )  AS [Filter4]) AS [C2]
FROM ( SELECT DISTINCT 
    [Filter1].[Prop3] AS [Prop3], 
    1 AS [C1]
    FROM ( SELECT [Extent1].[Prop3] AS [Prop3], [Extent3].[NavigationPropertyId] AS [NavigationPropertyId]
        FROM   [dbo].[anEntity] AS [Extent1]
      ))
    )  AS [Filter1]
 )  AS [Distinct1]

期望它在导航属性上使用连接执行单个select语句。还想选择多个属性,但每次都会添加另一个不必要的选项。关于如何简化生成的sql的任何想法?

0 个答案:

没有答案