0

I want convert following LINQ query to SQL query.

var ACTIVITY_ROYALITY_MONTH = from m in db.MiningPermit
                                      join pm in db.Permit_Mineral on m.MINING_PERMIT_ID equals pm.MINING_PERMIT_ID
                                      join r in db.Royality on pm.Permit_Minerals_ID equals r.Permit_Minerals_ID
                                      where r.ORDER_ID == 0 // NULL in server
                                      orderby r.YEAR, r.MONTH
                                      group r by new { m.MINING_PERMIT_ID , r.YEAR, r.MONTH }  into mpmr
                                      select mpmr.ToList();
Ishraq Ahmad
  • 1,049
  • 1
  • 12
  • 22
Ahmed Saif
  • 23
  • 1
  • 8
  • What have you tried so far? – Pavel Anikhouski Apr 07 '20 at 07:41
  • You can view actual query that is being executed. See this to find out how to view SQL query https://stackoverflow.com/questions/11002573/way-to-view-sql-executed-by-linq-in-visual-studio – Ishraq Ahmad Apr 07 '20 at 07:43
  • 1
    That's a bad LINQ query - LINQ isn't a replacement for SQL and the JOINs are generated by EF or L2S based on the context relations. What it does isn't what it looks like either - there's no way you can use `GROUP BY` and get *any* non-group column back without aggregation. What this really does is join some tables and return all rows and columns. You can copy it as-is, put `SELECT *` at the top and remove the `GROUP BY` clause – Panagiotis Kanavos Apr 07 '20 at 07:56

1 Answers1

1

Use Linqpad and recreate the linq (even by bringing in your assemblies) in a C# query. Run the query. Then in the output, there is a selection button of SQL which will show the sql code.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122