Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. SQL to LINQ Converter

SQL to LINQ Converter

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinqquestion
14 Posts 5 Posters 32 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi All; Does anyone know of any SQL to LINQ convertors around? Tnx Ian

    No trees were killed in the creation of this message. However, many electrons were terribly inconvenienced.

    C U 2 Replies Last reply
    0
    • L Lost User

      Hi All; Does anyone know of any SQL to LINQ convertors around? Tnx Ian

      No trees were killed in the creation of this message. However, many electrons were terribly inconvenienced.

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      iangrech wrote:

      Does anyone know of any SQL to LINQ convertors around?

      Ummm... Why would you want that? I don't know of any converters. And I am trying to figure out why you would want to do such a thing as there may be another solution.

      Upcoming FREE developer events: * Developer Day Scotland Recent blog posts: * Introduction to LINQ to XML (Part 1) - (Part 2) My website | Blog

      L 1 Reply Last reply
      0
      • C Colin Angus Mackay

        iangrech wrote:

        Does anyone know of any SQL to LINQ convertors around?

        Ummm... Why would you want that? I don't know of any converters. And I am trying to figure out why you would want to do such a thing as there may be another solution.

        Upcoming FREE developer events: * Developer Day Scotland Recent blog posts: * Introduction to LINQ to XML (Part 1) - (Part 2) My website | Blog

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        I am trying to understand LINQ and lambdas. I am trying to write a LINQ statement for SELECT DISTINCT [FieldName] FROM [TableName] ORDER BY [FieldName] where [TableName] is a datatable.

        No trees were killed in the creation of this message. However, many electrons were terribly inconvenienced.

        R 1 Reply Last reply
        0
        • L Lost User

          I am trying to understand LINQ and lambdas. I am trying to write a LINQ statement for SELECT DISTINCT [FieldName] FROM [TableName] ORDER BY [FieldName] where [TableName] is a datatable.

          No trees were killed in the creation of this message. However, many electrons were terribly inconvenienced.

          R Offline
          R Offline
          Roger Alsing 0
          wrote on last edited by
          #4

          First: You can not make it work like you described. You can not make linq operate on "made up" identifiers, the identifiers must exist on an a real class. so "Select distinct Foo" where foo is the name in a datatable wont work. you have to make a property called foo on some sort of object. That applies to both lambdas as delegates and lambdas as expression trees. eg: from row in DS["tablename"] order by row["fieldname"] select row["fieldname"] is valid, but: from row in tablename order by fieldname select fieldname is not.. no matter how much magic you try to do with expression trees. 2nd :) : Ive got a small clone of the Linq to objects engine: http://rogeralsing.com/2008/03/31/linq-to-objects-for-net-2-available/[^] you can always download that and customize it to work for datatables.

          Blog: http://www.rogeralsing.com Projects: http://www.puzzleframework.com

          L 1 Reply Last reply
          0
          • R Roger Alsing 0

            First: You can not make it work like you described. You can not make linq operate on "made up" identifiers, the identifiers must exist on an a real class. so "Select distinct Foo" where foo is the name in a datatable wont work. you have to make a property called foo on some sort of object. That applies to both lambdas as delegates and lambdas as expression trees. eg: from row in DS["tablename"] order by row["fieldname"] select row["fieldname"] is valid, but: from row in tablename order by fieldname select fieldname is not.. no matter how much magic you try to do with expression trees. 2nd :) : Ive got a small clone of the Linq to objects engine: http://rogeralsing.com/2008/03/31/linq-to-objects-for-net-2-available/[^] you can always download that and customize it to work for datatables.

            Blog: http://www.rogeralsing.com Projects: http://www.puzzleframework.com

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Hi Roger; Thank you for your reply but maybe I should have explained that what I put in [] is generic, ie:when I say [TableName], in real life [Tablename] will be a real tablename, eg: AccountTransactions Now back to the issue at hand. My problem is recreating this SQL statement as LINQ: SELECT DISTINCT [fieldname] FROM [tablename] ORDER BY [fieldname] I get as far as SELECT DISTINCT [fieldname] FROM [tablename] which translates to (from u in [tablename] select u.[fieldname]).Distinct() but the result is not sorted. I want to include the ORDER BY. Further to this, my o.p. was a request for a tool where I could write a SQL statement and it outputs the relevant LINQ, even if this is as lambda. I have LINQPad but it only does LINQ to SQL, not SQL to LINQ. Tnx Ian

            No trees were killed in the creation of this message. However, many electrons were terribly inconvenienced.

            P 1 Reply Last reply
            0
            • L Lost User

              Hi Roger; Thank you for your reply but maybe I should have explained that what I put in [] is generic, ie:when I say [TableName], in real life [Tablename] will be a real tablename, eg: AccountTransactions Now back to the issue at hand. My problem is recreating this SQL statement as LINQ: SELECT DISTINCT [fieldname] FROM [tablename] ORDER BY [fieldname] I get as far as SELECT DISTINCT [fieldname] FROM [tablename] which translates to (from u in [tablename] select u.[fieldname]).Distinct() but the result is not sorted. I want to include the ORDER BY. Further to this, my o.p. was a request for a tool where I could write a SQL statement and it outputs the relevant LINQ, even if this is as lambda. I have LINQPad but it only does LINQ to SQL, not SQL to LINQ. Tnx Ian

              No trees were killed in the creation of this message. However, many electrons were terribly inconvenienced.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              Well, you could do this as:

              (from u in ctx.tablename orderby ctx.field select ctx.field).Distinct();

              As far as I'm aware, there's no tool that reverses the process, i.e. from Sql to Linq.

              Deja View - the feeling that you've seen this post before.

              My blog | My articles

              L 1 Reply Last reply
              0
              • P Pete OHanlon

                Well, you could do this as:

                (from u in ctx.tablename orderby ctx.field select ctx.field).Distinct();

                As far as I'm aware, there's no tool that reverses the process, i.e. from Sql to Linq.

                Deja View - the feeling that you've seen this post before.

                My blog | My articles

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Tried it already. Output is unsorted.

                No trees were killed in the creation of this message. However, many electrons were terribly inconvenienced.

                P 1 Reply Last reply
                0
                • L Lost User

                  Tried it already. Output is unsorted.

                  No trees were killed in the creation of this message. However, many electrons were terribly inconvenienced.

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  Did you try orderby ... ascending?

                  Deja View - the feeling that you've seen this post before.

                  My blog | My articles

                  L 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    Did you try orderby ... ascending?

                    Deja View - the feeling that you've seen this post before.

                    My blog | My articles

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Yeppp. I even tried typing it while sacrificing a virgin (packet of orange juice).

                    No trees were killed in the creation of this message. However, many electrons were terribly inconvenienced.

                    P 1 Reply Last reply
                    0
                    • L Lost User

                      Yeppp. I even tried typing it while sacrificing a virgin (packet of orange juice).

                      No trees were killed in the creation of this message. However, many electrons were terribly inconvenienced.

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #10

                      Hmmm. Not sure what to suggest here, as this works OK for me. It might be worth SQL Profiling your app to see what's hitting the database.

                      Deja View - the feeling that you've seen this post before.

                      My blog | My articles

                      L 1 Reply Last reply
                      0
                      • P Pete OHanlon

                        Hmmm. Not sure what to suggest here, as this works OK for me. It might be worth SQL Profiling your app to see what's hitting the database.

                        Deja View - the feeling that you've seen this post before.

                        My blog | My articles

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        Using LINQPad, SQL Profiler shows that only a simple select (SELECT DISTINCT...FROM...) is passed to SQL Server, without the ORDER BY clause. I will try it from within an application later on. Tnx Ian

                        No trees were killed in the creation of this message. However, many electrons were terribly inconvenienced.

                        P 1 Reply Last reply
                        0
                        • L Lost User

                          Using LINQPad, SQL Profiler shows that only a simple select (SELECT DISTINCT...FROM...) is passed to SQL Server, without the ORDER BY clause. I will try it from within an application later on. Tnx Ian

                          No trees were killed in the creation of this message. However, many electrons were terribly inconvenienced.

                          P Offline
                          P Offline
                          Pete OHanlon
                          wrote on last edited by
                          #12

                          Hmm. Using LinqPAD, I ran the following query and it worked OK until I put the Distinct query in.

                          (from p in Customers
                          orderby p.Name
                          select new {p.ID, p.Name})

                          This produced the following SQL:

                          SELECT [t0].[ID], [t0].[Name]
                          FROM [Customer] AS [t0]
                          ORDER BY [t0].[Name]

                          As soon as I put the Distinct() in, I get:

                          SELECT DISTINCT [t0].[ID], [t0].[Name]
                          FROM [Customer] AS [t0]

                          which would seem to be a bug.

                          Deja View - the feeling that you've seen this post before.

                          My blog | My articles

                          L 1 Reply Last reply
                          0
                          • P Pete OHanlon

                            Hmm. Using LinqPAD, I ran the following query and it worked OK until I put the Distinct query in.

                            (from p in Customers
                            orderby p.Name
                            select new {p.ID, p.Name})

                            This produced the following SQL:

                            SELECT [t0].[ID], [t0].[Name]
                            FROM [Customer] AS [t0]
                            ORDER BY [t0].[Name]

                            As soon as I put the Distinct() in, I get:

                            SELECT DISTINCT [t0].[ID], [t0].[Name]
                            FROM [Customer] AS [t0]

                            which would seem to be a bug.

                            Deja View - the feeling that you've seen this post before.

                            My blog | My articles

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #13

                            Worked in code. Apparently LINQPad mishandles some queries then. So we agree: LINQPad developer will be tried, found guilty, tortured and executed. :) Tnx for your help.

                            No trees were killed in the creation of this message. However, many electrons were terribly inconvenienced.

                            modified on Tuesday, April 29, 2008 9:55 AM

                            1 Reply Last reply
                            0
                            • L Lost User

                              Hi All; Does anyone know of any SQL to LINQ convertors around? Tnx Ian

                              No trees were killed in the creation of this message. However, many electrons were terribly inconvenienced.

                              U Offline
                              U Offline
                              User 4088332
                              wrote on last edited by
                              #14

                              SQL to LINQ converter is available at www.sqltolinq.com

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups