[英]Select multiple joined tables into multiple variables in SQL queries or LINQ
SELECT O.*, U.*, C.*, P.*, S.*
INTO OV, Usuarios, Clientes, Proposta, Status
FROM[dbo].[TB_OV] O
INNER JOIN dbo.TB_Usuarios U on U.id = O.userinsert
INNER JOIN dbo.TB_Clientes C on C.id = O.ENDFAT
INNER JOIN dbo.TB_Status S on S.id = O.status
LEFT JOIN TB_Proposta P on P.id = O.ID_PROP
WHERE O.status = 214 ORDER BY O.DTSOL, O.DtdeFaturamento
I am developing a web system in C# and one of the queries that I am trying to do is this one... 我正在用C#开发Web系统,而我想做的查询之一就是这个。
I want to know what is the correct syntax to do that or something like this. 我想知道什么是正确的语法或类似的东西。
What I need to know exactly is how to select this five tables into five different variables like an object to each one that I can cast later in C# to turn into object typed. 我需要确切知道的是如何将这五个表选择为五个不同的变量,例如每个对象的一个对象,稍后我可以在C#中将其转换为对象类型。
Otherwise, I've tried to do it with LINQ to SQL from C# and I still did not get any results from queries in LINQ to SQL to these tables. 否则,我尝试使用C#中的LINQ to SQL来执行此操作,但仍然无法从LINQ to SQL到这些表的查询中获得任何结果。
Lambda: Lambda:
var query = db.TB_OV
.Join(db.TB_Usuarios, OV => OV.USERINSERT, U => U.ID, (OV, U) => new { OV, U })
.Join(db.TB_Clientes, Z => Z.OV.ENDFAT, CL => CL.ID, (Z, CL) => new { Z, CL })
.Join(db.TB_Status, Z => Z.Z.OV.STATUS, ST => ST.ID, (Z, ST) => new { Z, ST })
.Join(db.TB_Proposta, Z => Z.Z.Z.OV.ID_PROP, P => P.ID, (Z, P) => new { Z, P })
.Where(Z => Z.Z.Z.Z.OV.STATUS == 214)
.Select(Z => new OperacoesListaSolFaturamento
{
OV = Z.Z.Z.Z.OV,
Usuarios = Z.Z.Z.Z.U,
Clientes = Z.Z.Z.CL,
Status = Z.Z.ST,
Proposta = Z.P
});
Fluent: 流利:
var query = from O in db.TB_OV
join U in db.TB_Usuarios on O.USERINSERT equals U.ID
join C in db.TB_Clientes on O.ENDFAT equals C.ID
join S in db.TB_Status on O.STATUS equals S.ID
join P in db.TB_Proposta on O.ID_PROP equals P.ID
where O.STATUS == 214
select new OperacoesListaSolFaturamento
{
OV = O,
Usuarios = U,
Clientes = C,
Status = S,
Proposta = P
};
Is there any way to do that? 有什么办法吗? Either by LINQ or SQL queries. 通过LINQ或SQL查询。
For translating SQL to LINQ query comprehension: 要将SQL转换为LINQ查询理解:
FROM
subselects as separately declared variables. 将FROM
选择转换为单独声明的变量。 DISTINCT
, TOP
, MIN
, MAX
etc) into functions applied to the whole LINQ query. 以LINQ子句顺序转换每个子句,将单子运算符和集合运算符( DISTINCT
, TOP
, MIN
, MAX
等)转换为应用于整个LINQ查询的函数。 new {
... }
) for multiple columns. 对多个列使用匿名类型( new {
... }
)。 LEFT JOIN
is simulated by using into
joinvariable and doing another from from
the joinvariable followed by .DefaultIfEmpty()
. LEFT JOIN
通过使用模拟into
joinvariable和从做另一个from
所述joinvariable接着.DefaultIfEmpty()
COALESCE
with the conditional operator ( ?:
)and a null
test. 用条件运算符( ?:
COALESCE
和null
测试替换COALESCE
。 IN
to .Contains()
and NOT IN
to !
翻译IN
到.Contains()
和NOT IN
来!
... Contains()
. ... Contains()
BETWEEN
low AND
high to low <=
x &&
x <=
high . 翻译X BETWEEN
低 AND
高到低 <=
X &&
X <=
高 。 SELECT *
must be replaced with select range_variable or for joins, an anonymous object containing all the range variables. SELECT *
必须替换为select range_variable,或者对于联接来说,是包含所有范围变量的匿名对象。 SELECT
fields must be replaced with select new {
... }
creating an anonymous object with all the desired fields or expressions. SELECT
字段必须替换为select new {
... }
创建具有所有所需字段或表达式的匿名对象。 FULL OUTER JOIN
must be handled with an extension method. 正确的FULL OUTER JOIN
必须使用扩展方法来处理。 So the main problem with your fluent query is you didn't translate the LEFT JOIN
properly: 因此,您的流利查询的主要问题是您没有正确转换LEFT JOIN
:
var query = from O in db.TB_OV
where O.STATUS == 214
join U in db.TB_Usuarios on O.USERINSERT equals U.ID
join C in db.TB_Clientes on O.ENDFAT equals C.ID
join S in db.TB_Status on O.STATUS equals S.ID
join P in db.TB_Proposta on O.ID_PROP equals P.ID into Pj
from P in Pj.DefaultIfEmpty()
orderby O.DTSOL, O.DtdeFaturamento
select new OperacoesListaSolFaturamento
{
OV = O,
Usuarios = U,
Clientes = C,
Status = S,
Proposta = P
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.