运算符"AND"表示"OR"表示的SQL [英] Operators "AND" "OR" SQL
本文介绍了运算符"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:
- 将
FROM
子选择转换为单独声明的变量. - 按LINQ子句顺序转换每个子句,将单子运算符和聚合运算符(
DISTINCT
,TOP
,MIN
,MAX
等)转换为应用于整个LINQ查询的函数. - 使用表别名作为范围变量.使用列别名作为匿名类型字段名称.
- 对多列使用匿名类型(
new {
...}
).
并非所有使用 -
JOIN
条件,都必须使用联接外的where
子句或叉积(from
...from
...),然后使用where
在两个表之间进行多次 -
JOIN
条件应转换为匿名对象
通过使用 -
LEFT JOIN
. - 用条件运算符(
?:
)和null
测试替换COALESCE
. - 将
IN
转换为.Contains()
,将NOT IN
转换为!
...Contains()
. - 将 x
BETWEEN
lowAND
high 转换为 low<=
x&&
x<=
high . -
SELECT *
必须替换为select range_variable,或者对于联接来说,是包含所有范围变量的匿名对象. -
SELECT
字段必须替换为select new {
...}
创建具有所有所需字段或表达式的匿名对象. - 正确的
FULL OUTER JOIN
必须使用扩展方法来处理.
AND
进行相等性测试的AND
相等性测试的into
joinvariable 并从from
joinvariable 紧随其后的是.DefaultIfEmpty()
来模拟- Translate
FROM
subselects as separately declared variables. - 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. - Use table aliases as range variables. Use column aliases as anonymous type field names.
- Use anonymous types (
new {
...}
) for multiple columns. JOIN
conditions that aren't all equality tests withAND
must be handled usingwhere
clauses outside the join, or with cross product (from
...from
...) and thenwhere
JOIN
conditions that are multipleAND
ed equality tests between the two tables should be translated into anonymous objectsLEFT JOIN
is simulated by usinginto
joinvariable and doing another fromfrom
the joinvariable followed by.DefaultIfEmpty()
.- Replace
COALESCE
with the conditional operator (?:
)and anull
test. - Translate
IN
to.Contains()
andNOT IN
to!
...Contains()
. - Translate x
BETWEEN
lowAND
high to low<=
x&&
x<=
high. SELECT *
must be replaced with select range_variable or for joins, an anonymous object containing all the range variables.SELECT
fields must be replaced withselect new {
...}
creating an anonymous object with all the desired fields or expressions.- 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屋!
查看全文