【问题标题】:Translate SQL 'not in' into LINQ to Entities using Entity Framework 4使用 Entity Framework 4 将 SQL 'not in' 转换为 LINQ to Entities
【发布时间】:2015-12-12 01:07:40
【问题描述】:

如何使用 Entity Framework 4 将带有“not in”关键字的 SQL 转换为 LINQ to Entities? 所有以“ID”结尾的字段都是整数类型。

SELECT * 
        FROM IRSampleCompletionGoal as goal_tbl
  where 
  (Cast(goal_tbl.[TermID] as nvarchar) +' '+Cast(goal_tbl.[UserID] as nvarchar)+' '+Cast(goal_tbl.[CourseID] as nvarchar)+' '+cast(goal_tbl.[StudentID] as nvarchar)+' '+cast(goal_tbl.[ObjectiveID] as nvarchar))  
  NOT IN
  (
  select (Cast([TermID] as nvarchar) +' '+Cast([UserID] as nvarchar)+' '+Cast([CourseID] as nvarchar)+' '+cast([StudentID] as nvarchar)+' '+cast([ObjectiveID] as nvarchar)) 
  from IRSampleCompletionCurrentStatistics
  where ((ArtifactID is not null) OR (NoArtifactChecked='True' ))

  )

【问题讨论】:

  • 创建一个数组然后Not thisArray.Contains("somevalue")
  • !thisArray.Contains("somevalue") 用于 C#。

标签: c# sql-server vb.net entity-framework linq


【解决方案1】:

试试这样的:

 goal_tbl.Where(x => !IRSampleCompletionCurrentStatistics.Where(i => i.ArtifactID != null || i.NoArtifactChecked == true).Any(i => (i.TermID.ToString() + " " + i.UserID.ToString() + i.StudentID.ToString() + " " + i.ObjectiveID.ToString()) == (x.TermID.ToString()+ " " + x.UserID.ToString() + " " + x.CourseID.ToString() + " " + x.StudentID.ToString() + x.ObjectiveID.ToString()))

【讨论】:

  • 非常感谢所有响应者! Bruniasty,您能用 VB.NET 语法编写相同的 lambda 查询吗?如何将此查询的结果分配给变量?像这样:“Dim myvariable as IEnumerable(of IRSampleCompletionGoal) = 'your query'” ?
  • 我试过 Dim test As IEnumerable(Of IRSampleCompletionGoal) = goal_tbl.Where(!IRSampleCompletionCurrentStatistics.Where(i => i.ArtifactID != null || i.NoArtifactChecked == true).Any(i => (i.TermID.ToString() + " " + i.UserID.ToString() + i.StudentID.ToString() + " " + i.ObjectiveID.ToString()) == (x.TermID.ToString( )+ " " + x.UserID.ToString() + " " + x.CourseID.ToString() + " " + x.StudentID.ToString() + x.ObjectiveID.ToString())) 我得到了“预期的标识符i.ArtifactID 的错误!= null,指向“!=”符号”。
  • 尝试替换 'IsNot Nothing' 上的 '!= null',不要忘记在 lambda 表达式的开头添加 'x => '。
  • 如果可能的话,有人可以将原始查询转换为查询表达式语法而不是基于方法的语法吗?谢谢!
猜你喜欢
  • 1970-01-01
  • 2016-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-30
  • 2010-10-25
相关资源
最近更新 更多