使用Linq to Sql如何将下表(entidadeProdutosFornecedores)分组并从N个表中返回字段?Linq - 由多个表组成的多个表返回
原始查询在SQL
SELECT @NM_VALOR1 = MAX(ProdutosFornecedores.NM_PRECO_REPOSICAO),
@NM_VALOR2 = MAX(ProdutosFornecedores.ID_MOEDAS_REPOSICAO),
@ID_IMPOSTOSDESTINOS = MAX(Fornecedores.ID_IMPOSTOSDESTINOS),
@ID_IMPOSTOSCONFIG = MAX(ProdutosFornecedores.ID_IMPOSTOSCONFIG),
@ID_TABELANCMS = MAX(ProdutosFornecedores.ID_TABELANCMS),
@ID_FORNECEDORES = MAX(Fornecedores.ID_FORNECEDORES),
@CD_UF_BASE = MAX(UnidadesFederacao.CD_UNIDADEFEDERACAO)
FROM ProdutosFornecedores
INNER JOIN Fornecedores ON Fornecedores.ID_FORNECEDORES = ProdutosFornecedores.ID_FORNECEDORES
INNER JOIN Municipios ON Municipios.ID_MUNICIPIOS = Fornecedores.ID_MUNICIPIOS
INNER JOIN UnidadesFederacao ON UnidadesFederacao.ID_UNIDADESFEDERACAO = Municipios.ID_UNIDADESFEDERACAO
WHERE ProdutosFornecedores.ID_PRODUTOS = CAST(@CD_OBJETO1 AS INT) AND
ProdutosFornecedores.ID_PRODUTOSCONFIGPRECOS = CAST(@CD_OBJETO2 AS INT) AND
ProdutosFornecedores.FG_STATUS = 1
GROUP BY
ProdutosFornecedores.ID_PRODUTOS,
ProdutosFornecedores.ID_PRODUTOSCONFIGPRECOS
查询转换为LINQ的
var prodForn2 = from entidadeProdutosFornecedores in ERPDAOManager.GetTable<ProdutosFornecedores>()
//Inner Join with Fornecedores
join entidadeFornecedores in ERPDAOManager.GetTable<Fornecedores>()
on entidadeProdutosFornecedores.ID_FORNECEDORES equals entidadeFornecedores.ID into tempFornecedores
from fornecedores in tempFornecedores
//Inner Join with Municipios
join entidadeMuncipios in ERPDAOManager.GetTable<Municipios>()
on fornecedores.ID_MUNICIPIOS equals entidadeMuncipios.ID into tempMunicipios
from municipios in tempMunicipios
//Inner Join with UnidadesFederacao
join entidadeUnidadesFederacao in ERPDAOManager.GetTable<UnidadesFederacao>()
on municipios.ID_UNIDADESFEDERACAO equals entidadeUnidadesFederacao.ID into tempUnidadesFederacao
from unidadesFederacao in tempUnidadesFederacao
//Filters
where entidadeProdutosFornecedores.ID_PRODUTOS == Convert.ToInt32(objEsquemasCalculoRegras.CD_OBJETO1) &&
entidadeProdutosFornecedores.ID_PRODUTOSCONFIGPRECOS == Convert.ToInt32(objEsquemasCalculoRegras.CD_OBJETO2) &&
entidadeProdutosFornecedores.FG_STATUS == true
group entidadeProdutosFornecedores by new { entidadeProdutosFornecedores.ID_PRODUTOS, entidadeProdutosFornecedores.ID_PRODUTOSCONFIGPRECOS } into produtosFornecedores
select new
{
NM_PRECO_REPOSICAO = (decimal)produtosFornecedores.Max(item => item.NM_PRECO_REPOSICAO),
ID_MOEDAS_REPOSICAO = (int)produtosFornecedores.Max(item => item.ID_MOEDAS_REPOSICAO),
ID_IMPOSTOSDESTINOS = (int)fornecedores.ID_IMPOSTOSDESTINOS, //Error: The name fornecedores does not exist in the current context
ID_IMPOSTOSCONFIG = (int)produtosFornecedores.Max(item => item.ID_IMPOSTOSCONFIG),
ID_TABELANCMS = (int)produtosFornecedores.Max(item => item.ID_TABELANCMS),
ID_FORNECEDORES = (int)fornecedores.ID, //Error: The name fornecedores does not exist in the current context
CD_UF_BASE = (string)unidadesFederacao.CD_UNIDADEFEDERACAO //Error: The name unidadesFederacao does not exist in the current context
};
嗨家伙,谢谢。我已经将所有字段分组,并且我按照您的选择(2)进行了分类。 – Gandarez