LINQ不读括号

时间:2014-01-17 16:26:27

标签: c# sql linq entity-framework

这是我的LINQ(EF):

var ls = from ps in ctx.Package_Services
     where 
        (ps.Package.Policy.Name != "Short-Term Career Policy"
            && ps.Package.Name != "Early Talent Package")
        &&
        (ps.Package.Policy.Name != "Assignment Policy"
            && ps.Package.Name != "Efficient Package")
     select new
     {
         Service = ps.Service,
         PackageService = ps
     };

这是它生成的相关SQL:

WHERE (N'Early Talent Package' <> [Extent2].[Name]) 
AND (N'Efficient Package' <> [Extent2].[Name]) 
AND (N'Short-Term Career Policy' <> [Extent3].[Name]) 
AND (N'Assignment Policy' <> [Extent4].[Name])

我想说的是,“我不想要组合X,我也不想要组合Y;但是,我确实想要”早期人才套餐“,它们与”短期“无关职业政策。“而且,当他们不依赖于”分配政策“时,我也想要”高效套餐“。

如何将其转换为LINQ?

3 个答案:

答案 0 :(得分:5)

它可能会消除它们,因为它不需要它们。

你实际上是在说“不是这个”“不是这个”“不是这个”“不是这个”。< / p>

现在,如果中间有“OR”,则必须将它们考虑在内:

(ps.Package.Policy.Name != "Short-Term Career Policy"
    && ps.Package.Name != "Early Talent Package")
||
(ps.Package.Policy.Name != "Assignment Policy"
    && ps.Package.Name != "Efficient Package")

答案 1 :(得分:2)

根据你所说的你想要实现的目标:

  

我不想要组合X和我也不想要组合Y;   但是,我确实希望“早期人才套餐”与他们没有关系   “短期职业政策。”并且,我也想要“高效套餐”   它们与“作业政策”无关。

我认为你的布尔逻辑一般都是有缺陷的。

短期职业政策短期职业政策无关时,您需要早期人才待遇

((ps.Package.Policy.Name == "Early Talent Package") 
 && (ps.Package.Policy.Name != "Short-Term Career Policy"))

OR(||

如果不依据分配政策,您需要高效套餐

((ps.Package.Name == "Efficient Package") 
 && (ps.Package.Policy.Name != "Assignment Policy"))

所有在一起:

((ps.Package.Policy.Name == "Early Talent Package") 
    && (ps.Package.Policy.Name != "Short-Term Career Policy"))
||
((ps.Package.Name == "Efficient Package") 
    && (ps.Package.Policy.Name != "Assignment Policy"))

答案 2 :(得分:0)

这对我有用。

var ls = from ps in ctx.Package_Services
         where 
            !(
                (ps.Package.Policy.Name == "Short-Term Career Policy"
                    && ps.Package.Name == "Early Talent Package")
                ||
                (ps.Package.Policy.Name == "Assignment Policy"
                    && ps.Package.Name == "Efficient Package")
            )
         select new
         {
             Service = ps.Service,
             PackageService = ps
         };

它生成的SQL就像这样:

SELECT 
1 AS [C1], 
[Extent4].[Id] AS [Id], 
[Extent4].[Name] AS [Name], 
[Filter1].[Id1] AS [Id1], 
[Filter1].[PackageId] AS [PackageId], 
[Filter1].[ServiceId] AS [ServiceId], 
[Filter1].[SectionId] AS [SectionId], 
[Filter1].[Mandatory] AS [Mandatory]
FROM   (SELECT [Extent1].[Id] AS [Id1], [Extent1].[PackageId] AS [PackageId], [Extent1].[ServiceId] AS [ServiceId], [Extent1].[SectionId] AS [SectionId], [Extent1].[Mandatory] AS [Mandatory]
    FROM   [dbo].[Package_Services] AS [Extent1]
    INNER JOIN [dbo].[Packages] AS [Extent2] ON [Extent1].[PackageId] = [Extent2].[Id]
    INNER JOIN [dbo].[Policy] AS [Extent3] ON [Extent2].[PolicyId] = [Extent3].[Id]
    WHERE ( NOT (((N'Short-Term Career Policy' = [Extent3].[Name]) AND (N'Early Talent Package' = [Extent2].[Name])) OR ((N'Assignment Policy' = [Extent3].[Name]) AND (N'Efficient Package' = [Extent2].[Name])))) AND (N'Exception Policy' <> [Extent3].[Name]) ) AS [Filter1]
INNER JOIN [dbo].[Services] AS [Extent4] ON [Filter1].[ServiceId] = [Extent4].[Id]
WHERE N'Transportation' = [Extent4].[Name]