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. Database & SysAdmin
  3. Database
  4. what is the equivalent linq query for the following sql query?

what is the equivalent linq query for the following sql query?

Scheduled Pinned Locked Moved Database
databasequestioncsharplinqhelp
7 Posts 3 Posters 0 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.
  • D Offline
    D Offline
    Dhyanga
    wrote on last edited by
    #1

    string qryInterviews = "SELECT ID, UpdatedByAdmin, UpdatedBy,Deleted " +
    "CASE UpdatedByAdmin WHEN 1 THEN Date1 ELSE TimesForDate1 END AS D1, " +
    "CASE UpdatedByAdmin WHEN 1 THEN Date2 ELSE TimesForDate2 END AS D2, " +
    "CASE UpdatedByAdmin WHEN 1 THEN Date3 ELSE TimesForDate3 END AS D3, " +

    "FROM myTable where (Deleted is null OR Deleted = 0)";

    In the above query, UpdatedByAdmin,Deleted are of tinyInt datatype. Please help.

    Dhyanga

    P L 2 Replies Last reply
    0
    • D Dhyanga

      string qryInterviews = "SELECT ID, UpdatedByAdmin, UpdatedBy,Deleted " +
      "CASE UpdatedByAdmin WHEN 1 THEN Date1 ELSE TimesForDate1 END AS D1, " +
      "CASE UpdatedByAdmin WHEN 1 THEN Date2 ELSE TimesForDate2 END AS D2, " +
      "CASE UpdatedByAdmin WHEN 1 THEN Date3 ELSE TimesForDate3 END AS D3, " +

      "FROM myTable where (Deleted is null OR Deleted = 0)";

      In the above query, UpdatedByAdmin,Deleted are of tinyInt datatype. Please help.

      Dhyanga

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      That doesn't even look like valid SQL.

      D 2 Replies Last reply
      0
      • P PIEBALDconsult

        That doesn't even look like valid SQL.

        D Offline
        D Offline
        Dhyanga
        wrote on last edited by
        #3

        this is the currently running query in my project. I don't know how columnname with tinyInt datatype be compared to any given value in the case statement in linq.

        Dhyanga

        1 Reply Last reply
        0
        • P PIEBALDconsult

          That doesn't even look like valid SQL.

          D Offline
          D Offline
          Dhyanga
          wrote on last edited by
          #4

          var query = (from sub in db.myTable
          where (sub.Deleted == null || sub.Deleted == 0)
          select new
          {
          sub.ID,
          sub.UpdatedByAdmin,
          sub.UpdatedBy,
          sub.Deleted,
          D1 = sub.UpdatedByAdmin.Equals(true ? sub.Date1 : sub.TimesForDate1),
          D2 = sub.UpdatedByAdmin.Equals(true ? sub.Date2 : sub.TimesForDate2),
          D3 = sub.UpdatedByAdmin.Equals(true ? sub.Date3 : sub.TimesForDate3)

                                   }).ToList();
          

          But this is giving error message as:

          System.ArgumentException: DbComparisonExpression requires arguments with comparable types.

          I think its because my UpdatedByAdmin, Deleted columns are tinyInt datatype in the database. But When i used these fields in model class, i used byte datatype. I thought byte is equivalent to tinyInt. Please help.

          Dhyanga

          P 1 Reply Last reply
          0
          • D Dhyanga

            var query = (from sub in db.myTable
            where (sub.Deleted == null || sub.Deleted == 0)
            select new
            {
            sub.ID,
            sub.UpdatedByAdmin,
            sub.UpdatedBy,
            sub.Deleted,
            D1 = sub.UpdatedByAdmin.Equals(true ? sub.Date1 : sub.TimesForDate1),
            D2 = sub.UpdatedByAdmin.Equals(true ? sub.Date2 : sub.TimesForDate2),
            D3 = sub.UpdatedByAdmin.Equals(true ? sub.Date3 : sub.TimesForDate3)

                                     }).ToList();
            

            But this is giving error message as:

            System.ArgumentException: DbComparisonExpression requires arguments with comparable types.

            I think its because my UpdatedByAdmin, Deleted columns are tinyInt datatype in the database. But When i used these fields in model class, i used byte datatype. I thought byte is equivalent to tinyInt. Please help.

            Dhyanga

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Dhyanga wrote:

            I thought byte is equivalent to tinyInt.

            It is, but SQL tends to be more forgiving than C#.

            1 Reply Last reply
            0
            • D Dhyanga

              string qryInterviews = "SELECT ID, UpdatedByAdmin, UpdatedBy,Deleted " +
              "CASE UpdatedByAdmin WHEN 1 THEN Date1 ELSE TimesForDate1 END AS D1, " +
              "CASE UpdatedByAdmin WHEN 1 THEN Date2 ELSE TimesForDate2 END AS D2, " +
              "CASE UpdatedByAdmin WHEN 1 THEN Date3 ELSE TimesForDate3 END AS D3, " +

              "FROM myTable where (Deleted is null OR Deleted = 0)";

              In the above query, UpdatedByAdmin,Deleted are of tinyInt datatype. Please help.

              Dhyanga

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

              In this case, the query is more readable than the LINQ version. Secondly, this query executes on the server. What use does changing it to LINQ have?

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

              D 1 Reply Last reply
              0
              • L Lost User

                In this case, the query is more readable than the LINQ version. Secondly, this query executes on the server. What use does changing it to LINQ have?

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                D Offline
                D Offline
                Dhyanga
                wrote on last edited by
                #7

                I am trying to implement entity framework to that project..

                Dhyanga

                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