Entitiy Framework参数化列表<long>

时间:2018-03-15 07:39:45

标签: sql-server c# entity-framework linq

我们正在使用EF 6.2 for .NET。我们正在尝试下一个查询:

dbContext.memberScales
.Where(s => members.Contains(s.idMember))
.OrderByDescending(s => s.dateScale)
.Select(s => new MemberScaleBasicFields
{
    id = s.idMember,
    dateScale = s.dateScale,
    weight = s.weight,
    massMuscle = s.massMuscle,
    massFat = s.massFat,
    massBone = s.massBone,
    imc = s.imc,
    water = s.water,
    dailyCalories = s.dailyCalories,
    tmd = s.tmd,
    physicalValuation = s.physicalValuation,
    adiposity = s.adiposity,
    assessment = s.assessment,
    ageMetabolica = s.ageMetabolica
})
.ToList();

在where子句中,我们必须过滤long列表。

members.Contains(s.idMember)

当我们看到查询文本时,我们会得到一个包含硬核值的查询:

SELECT 
[Extent1].[ValoracionFisica] AS [ValoracionFisica], 
[Extent1].[IdSocio] AS [IdSocio], 
[Extent1].[Fecha] AS [Fecha], 
[Extent1].[Peso] AS [Peso], 
[Extent1].[MasaMagra] AS [MasaMagra], 
[Extent1].[MasaGrasa] AS [MasaGrasa], 
[Extent1].[MasaOsea] AS [MasaOsea], 
[Extent1].[IMC] AS [IMC], 
[Extent1].[Agua] AS [Agua], 
[Extent1].[CaloriasDiarias] AS [CaloriasDiarias], 
[Extent1].[TMB] AS [TMB], 
[Extent1].[Adiposidad] AS [Adiposidad], 
[Extent1].[Valoracion] AS [Valoracion], 
[Extent1].[EdadMetabolica] AS [EdadMetabolica]
FROM [dbo].[Socios_Bascula] AS [Extent1]
WHERE [Extent1].[IdSocio] IN (cast(1225789 as bigint), cast(1228549 as bigint), cast(1228557 as bigint), cast(1230732 as bigint)....

我们想知道如何使这个查询参数化。或者,如果有任何替代方法可以进行此查询而不删除缓存计划。例如,

[Extent1].[IdSocio] IN (@value1, @value2...)

如果我们尝试制作

members.Any(x => x == s.idMember)

我们得到下一个文本

SELECT 
[Extent1].[ValoracionFisica] AS [ValoracionFisica], 
[Extent1].[IdSocio] AS [IdSocio], 
[Extent1].[Fecha] AS [Fecha], 
[Extent1].[Peso] AS [Peso], 
[Extent1].[MasaMagra] AS [MasaMagra], 
[Extent1].[MasaGrasa] AS [MasaGrasa], 
[Extent1].[MasaOsea] AS [MasaOsea], 
[Extent1].[IMC] AS [IMC], 
[Extent1].[Agua] AS [Agua], 
[Extent1].[CaloriasDiarias] AS [CaloriasDiarias], 
[Extent1].[TMB] AS [TMB], 
[Extent1].[Adiposidad] AS [Adiposidad], 
[Extent1].[Valoracion] AS [Valoracion], 
[Extent1].[EdadMetabolica] AS [EdadMetabolica]
FROM [dbo].[Socios_Bascula] AS [Extent1]
WHERE  EXISTS (SELECT 
    1 AS [C1]
    FROM  (SELECT 
        cast(1225789 as bigint) AS [C1]
        FROM  ( SELECT 1 AS X ) AS [SingleRowTable1]
    UNION ALL
        SELECT 
        cast(1228549 as bigint) AS [C1]
        FROM  ( SELECT 1 AS X ) AS [SingleRowTable2]....

0 个答案:

没有答案