Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq - Group by multiple tables with multiple returns
    primarykey
    data
    text
    <p>Using Linq to Sql how do I group the following table (entidadeProdutosFornecedores) and return fields from N tables ?</p> <p>Original Query in SQL</p> <pre><code>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 </code></pre> <p>Query converted to Linq</p> <pre><code>var prodForn2 = from entidadeProdutosFornecedores in ERPDAOManager.GetTable&lt;ProdutosFornecedores&gt;() //Inner Join with Fornecedores join entidadeFornecedores in ERPDAOManager.GetTable&lt;Fornecedores&gt;() on entidadeProdutosFornecedores.ID_FORNECEDORES equals entidadeFornecedores.ID into tempFornecedores from fornecedores in tempFornecedores //Inner Join with Municipios join entidadeMuncipios in ERPDAOManager.GetTable&lt;Municipios&gt;() on fornecedores.ID_MUNICIPIOS equals entidadeMuncipios.ID into tempMunicipios from municipios in tempMunicipios //Inner Join with UnidadesFederacao join entidadeUnidadesFederacao in ERPDAOManager.GetTable&lt;UnidadesFederacao&gt;() on municipios.ID_UNIDADESFEDERACAO equals entidadeUnidadesFederacao.ID into tempUnidadesFederacao from unidadesFederacao in tempUnidadesFederacao //Filters where entidadeProdutosFornecedores.ID_PRODUTOS == Convert.ToInt32(objEsquemasCalculoRegras.CD_OBJETO1) &amp;&amp; entidadeProdutosFornecedores.ID_PRODUTOSCONFIGPRECOS == Convert.ToInt32(objEsquemasCalculoRegras.CD_OBJETO2) &amp;&amp; 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 =&gt; item.NM_PRECO_REPOSICAO), ID_MOEDAS_REPOSICAO = (int)produtosFornecedores.Max(item =&gt; 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 =&gt; item.ID_IMPOSTOSCONFIG), ID_TABELANCMS = (int)produtosFornecedores.Max(item =&gt; 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 }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload