Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6223621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:31:19+00:00 2026-05-24T08:31:19+00:00

Using Linq to Sql how do I group the following table (entidadeProdutosFornecedores) and return

  • 0

Using Linq to Sql how do I group the following table (entidadeProdutosFornecedores) and return fields from N tables ?

Original Query in 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

Query converted to 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
                                                    };
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:31:20+00:00Added an answer on May 24, 2026 at 8:31 am

    The issue you’re getting with your query is that you’re trying to access “from” variables after the “group by” which you can’t do. In order to get these variables they must be either (1) kept outside of the grouping or (2) made into part of the grouping itself.

    (1)

    var prodForn2 =
        from produtosFornecedores in ERPDAOManager.GetTable<ProdutosFornecedores>()
        join fornecedores in ERPDAOManager.GetTable<Fornecedores>()
            on produtosFornecedores.ID_FORNECEDORES equals fornecedores.ID
        join municipios in ERPDAOManager.GetTable<Municipios>()
            on fornecedores.ID_MUNICIPIOS equals municipios.ID
        join unidadesFederacao in ERPDAOManager.GetTable<UnidadesFederacao>()
            on municipios.ID_UNIDADESFEDERACAO equals unidadesFederacao.ID
        where produtosFornecedores.ID_PRODUTOS ==
            Convert.ToInt32(objEsquemasCalculoRegras.CD_OBJETO1)
        where produtosFornecedores.ID_PRODUTOSCONFIGPRECOS ==
            Convert.ToInt32(objEsquemasCalculoRegras.CD_OBJETO2)
        where produtosFornecedores.FG_STATUS == true
        let ID_IMPOSTOSDESTINOS = (int)fornecedores.ID_IMPOSTOSDESTINOS
        let ID_FORNECEDORES = (int)fornecedores.ID
        let CD_UF_BASE = (string)unidadesFederacao.CD_UNIDADEFEDERACAO
        group produtosFornecedores by new
        {
            ID_IMPOSTOSDESTINOS,
            ID_FORNECEDORES,
            CD_UF_BASE,
        } into gpfs1
        select new
        {
            gpfs1.Key.ID_IMPOSTOSDESTINOS,
            gpfs1.Key.ID_FORNECEDORES,
            gpfs1.Key.CD_UF_BASE,
            PRODUTOSFORNECEDORES =
                from pfs1 in gpfs1
                group pfs1 by new
                {
                    pfs1.ID_PRODUTOS,
                    pfs1.ID_PRODUTOSCONFIGPRECOS
                } into gpfs2
                select new
                {
                    NM_PRECO_REPOSICAO = (decimal)gpfs2
                        .Max(item => item.NM_PRECO_REPOSICAO),
                    ID_MOEDAS_REPOSICAO = (int)gpfs2
                        .Max(item => item.ID_MOEDAS_REPOSICAO),
                    ID_IMPOSTOSCONFIG = (int)gpfs2
                        .Max(item => item.ID_IMPOSTOSCONFIG),
                    ID_TABELANCMS = (int)gpfs2
                        .Max(item => item.ID_TABELANCMS),
                },
        };
    

    (2)

    var prodForn2 =
        from produtosFornecedores in ERPDAOManager.GetTable<ProdutosFornecedores>()
        join fornecedores in ERPDAOManager.GetTable<Fornecedores>()
            on produtosFornecedores.ID_FORNECEDORES equals fornecedores.ID
        join municipios in ERPDAOManager.GetTable<Municipios>()
            on fornecedores.ID_MUNICIPIOS equals municipios.ID
        join unidadesFederacao in ERPDAOManager.GetTable<UnidadesFederacao>()
            on municipios.ID_UNIDADESFEDERACAO equals unidadesFederacao.ID
        where produtosFornecedores.ID_PRODUTOS ==
            Convert.ToInt32(objEsquemasCalculoRegras.CD_OBJETO1)
        where produtosFornecedores.ID_PRODUTOSCONFIGPRECOS ==
            Convert.ToInt32(objEsquemasCalculoRegras.CD_OBJETO2)
        where produtosFornecedores.FG_STATUS == true
        let ID_IMPOSTOSDESTINOS = (int)fornecedores.ID_IMPOSTOSDESTINOS
        let ID_FORNECEDORES = (int)fornecedores.ID
        let CD_UF_BASE = (string)unidadesFederacao.CD_UNIDADEFEDERACAO
        group produtosFornecedores by new
        {
            ID_IMPOSTOSDESTINOS,
            ID_FORNECEDORES,
            CD_UF_BASE,
            produtosFornecedores.ID_PRODUTOS,
            produtosFornecedores.ID_PRODUTOSCONFIGPRECOS,
        } into gpfs
        select new
        {
            gpfs.Key.ID_IMPOSTOSDESTINOS,
            gpfs.Key.ID_FORNECEDORES,
            gpfs.Key.CD_UF_BASE,
            NM_PRECO_REPOSICAO = (decimal)gpfs.Max(item => item.NM_PRECO_REPOSICAO),
            ID_MOEDAS_REPOSICAO = (int)gpfs.Max(item => item.ID_MOEDAS_REPOSICAO),
            ID_IMPOSTOSCONFIG = (int)gpfs.Max(item => item.ID_IMPOSTOSCONFIG),
            ID_TABELANCMS = (int)gpfs.Max(item => item.ID_TABELANCMS),
        };
    

    Give each of these a go and see which suits your needs better.

    You may also find that performance is an issue with grouping and the multiple max queries, so it might be worth your while bringing the records into memory before grouping the results.

    var prodForn2_1 =
        from produtosFornecedores in ERPDAOManager.GetTable<ProdutosFornecedores>()
        join fornecedores in ERPDAOManager.GetTable<Fornecedores>()
            on produtosFornecedores.ID_FORNECEDORES equals fornecedores.ID
        join municipios in ERPDAOManager.GetTable<Municipios>()
            on fornecedores.ID_MUNICIPIOS equals municipios.ID
        join unidadesFederacao in ERPDAOManager.GetTable<UnidadesFederacao>()
            on municipios.ID_UNIDADESFEDERACAO equals unidadesFederacao.ID
        where produtosFornecedores.ID_PRODUTOS ==
            Convert.ToInt32(objEsquemasCalculoRegras.CD_OBJETO1)
        where produtosFornecedores.ID_PRODUTOSCONFIGPRECOS ==
            Convert.ToInt32(objEsquemasCalculoRegras.CD_OBJETO2)
        where produtosFornecedores.FG_STATUS == true
        let ID_IMPOSTOSDESTINOS = (int)fornecedores.ID_IMPOSTOSDESTINOS
        let ID_FORNECEDORES = (int)fornecedores.ID
        let CD_UF_BASE = (string)unidadesFederacao.CD_UNIDADEFEDERACAO
        select new
        {
            ID_IMPOSTOSDESTINOS = (int)fornecedores.ID_IMPOSTOSDESTINOS,
            ID_FORNECEDORES = (int)fornecedores.ID,
            CD_UF_BASE = (string)unidadesFederacao.CD_UNIDADEFEDERACAO,
            ID_PRODUTOS = produtosFornecedores.ID_PRODUTOS,
            ID_PRODUTOSCONFIGPRECOS = produtosFornecedores.ID_PRODUTOSCONFIGPRECOS,
            NM_PRECO_REPOSICAO = (decimal)produtosFornecedores.NM_PRECO_REPOSICAO,
            ID_MOEDAS_REPOSICAO = (int)produtosFornecedores.ID_MOEDAS_REPOSICAO,
            ID_IMPOSTOSCONFIG = (int)produtosFornecedores.ID_IMPOSTOSCONFIG,
            ID_TABELANCMS = (int)produtosFornecedores.ID_TABELANCMS,
        };
    
    var prodForn2_2 =
        from pf in prodForn2_1.ToArray()
        group ...
    

    Now you just need to complete the prodForn2_2 with either of option (1) or (2) from above. Note that the ToArray call will force the prodForn2_1 query to execute and bring the records into memory as an array – grouping and sub-querying is then lightningly fast. You just need to watch out on memory use rather than query execution time.

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Linq to Sql how do i group the following 2 tables. Orders Table
How to select all columns from tables in join using linq Sql: select CTRL_RUN_JOB.*,
How would the following sql query look when translated to linq? SELECT myId, Count(myId)
I´m using linq to sql and I have a lot of tables with foreign
I have the following query: SELECT S.[FlowOrder], S.[DESCRIPTION], COUNT(I.ID) FROM WorkFlowStatus AS S INNER
I am using LINQ to SQL in my project. I have two tables Category
I've got a T-SQL query similar to this: SELECT r_id, r_name, count(*) FROM RoomBindings
I have the following SQL query to return all Customers who have no OrderLines
I am struggling with using LINQ to SQL XML. I'm following examples and getting
I'm having a bit of issue converting the following t-sql statement to Linq (using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.