运算符"AND"表示"OR"表示的SQL [英] Operators "AND" "OR" SQL

查看:107
本文介绍了运算符"AND"表示"OR"表示的SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试使用Linq将此代码从SQL转换为C#

Hello I'm trying to transform this code from SQL to C# using Linq

SQL代码如下:

SELECT N.Name,N.Unit,N.Typee,ID,NOTE 
FROM Table_A N join Table_B A on (N.ID = A.ID) 
join NotasQL G on ((N.Not1 = G.CODE) or (N.Not2 = G.CODE) ) 
join Attributes X on (A.AppID= X.AppID)

此代码在SQL中可以正常运行,并具有预期的结果,但是当我尝试在C#上复制此代码时,我不知道如何执行或"部分,这就是我到目前为止所拥有的:

This Code is running fine in SQL with the expected result, but when I'm trying to replicate this on C# I don't know how to do the OR part, this is what I have so far:

var Select = (from A in context.Table_A 
              from B in context.Table_B 
              from E in context.NotasQLs
              from D in context.Attributes 
             where (String.Compare(A.ID, B.ID, true) == 0 &&
                    String.Compare(B.AppID, D.AppID, true) == 0 
                     &&
                    (String.Compare(A.Not1, E.CODE, true) == 0 || 
                      String.Compare(A.Not2, E.CODE, true) == 0))

如果我删除了or条件运行但我需要OR,则我的应用程序运行时已过期,因为查询没有选择任何内容.

I'm having an application runtime expired because the query is not selecting nothing, if I remove the or condition runs but I need the OR.

推荐答案

用于将SQL转换为LINQ查询理解:

For translating SQL to LINQ query comprehension:

  1. FROM子选择转换为单独声明的变量.
  2. 按LINQ子句顺序转换每个子句,将单子运算符和聚合运算符(DISTINCTTOPMINMAX等)转换为应用于整个LINQ查询的函数.
  3. 使用表别名作为范围变量.使用列别名作为匿名类型字段名称.
  4. 对多列使用匿名类型(new { ... }).
  5. 并非所有使用AND进行相等性测试的
  6. JOIN条件,都必须使用联接外的where子句或叉积(from ... from ...),然后使用where
  7. 在两个表之间进行多次AND相等性测试的
  8. JOIN条件应转换为匿名对象
  9. 通过使用into joinvariable 并从from joinvariable 紧随其后的是.DefaultIfEmpty()来模拟
  10. LEFT JOIN.
  11. 用条件运算符(?:)和null测试替换COALESCE.
  12. IN转换为.Contains(),将NOT IN转换为! ... Contains().
  13. x BETWEEN low AND high 转换为 low <= x && x <= high .
  14. SELECT *必须替换为select range_variable,或者对于联接来说,是包含所有范围变量的匿名对象.
  15. SELECT字段必须替换为select new { ... }创建具有所有所需字段或表达式的匿名对象.
  16. 正确的FULL OUTER JOIN必须使用扩展方法来处理.
  1. Translate FROM subselects as separately declared variables.
  2. Translate each clause in LINQ clause order, translating monadic and aggregate operators (DISTINCT, TOP, MIN, MAX etc) into functions applied to the whole LINQ query.
  3. Use table aliases as range variables. Use column aliases as anonymous type field names.
  4. Use anonymous types (new { ... }) for multiple columns.
  5. JOIN conditions that aren't all equality tests with AND must be handled using where clauses outside the join, or with cross product (from ... from ...) and then where
  6. JOIN conditions that are multiple ANDed equality tests between the two tables should be translated into anonymous objects
  7. LEFT JOIN is simulated by using into joinvariable and doing another from from the joinvariable followed by .DefaultIfEmpty().
  8. Replace COALESCE with the conditional operator (?:)and a null test.
  9. Translate IN to .Contains() and NOT IN to !...Contains().
  10. Translate x BETWEEN low AND high to low <= x && x <= high.
  11. SELECT * must be replaced with select range_variable or for joins, an anonymous object containing all the range variables.
  12. SELECT fields must be replaced with select new { ... } creating an anonymous object with all the desired fields or expressions.
  13. Proper FULL OUTER JOIN must be handled with an extension method.

对于您的查询,

var ans = from N in Table_A
          join A in Table_B on N.ID equals A.ID
          from G in NotasQL
          where G.CODE == N.Not1 || G.CODE == N.Not2
          join X in Attributes on A.AppID equals X.AppID
          select new {
              N.Name,
              N.Unit,
              N.Typee,
              N.ID, // ??? not sure table for this column
              G.NOTE // ??? not sure table for this column
          };

这篇关于运算符"AND"表示"OR"表示的SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆