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. Some help with SQL to Linq Conversions

Some help with SQL to Linq Conversions

Scheduled Pinned Locked Moved Database
linqcsharpdatabasehelp
5 Posts 2 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.
  • J Offline
    J Offline
    jkirkerx
    wrote on last edited by
    #1

    I'm trying to convert this to Linq. I didn't see a OR in Linq, so I'm figuring you can do multiple Where stateements. And on the date, I'm not sure if I should calculate it in code and jsut submit the date, or if there is a way to use Linq to do Date Calcs.

    " SELECT COUNT(*) " & _
    " FROM CRM_MESSAGES " & _
    " WHERE " & _
    " MessageStatus='MESSAGE_COMPLETE' " & _
    " OR MessageStatus='MESSAGE_PENDING' " & _
    " OR MessageStatus='MESSAGE_FORWARD' " & _
    " AND ContactDate >=DATEADD(day, -30, getdate()) "

    So Far I've got this

    Dim messages As IQueryable(Of CRM_MESSAGES)
    messages = messages.Where(Function(m) m.MessageStatus = "MESSAGE_COMPLETE")
    messages = messages.Where(Function(m) m.MessageStatus = "MESSAGE_PENDING")
    messages = messages.Where(Function(m) m.MessageStatus = "MESSAGE_FORWARD")
    messages = messages.Where(Function(m) m.ContactDate >= m.ContactDate.Date.AddDays(-30))

    Richard DeemingR 1 Reply Last reply
    0
    • J jkirkerx

      I'm trying to convert this to Linq. I didn't see a OR in Linq, so I'm figuring you can do multiple Where stateements. And on the date, I'm not sure if I should calculate it in code and jsut submit the date, or if there is a way to use Linq to do Date Calcs.

      " SELECT COUNT(*) " & _
      " FROM CRM_MESSAGES " & _
      " WHERE " & _
      " MessageStatus='MESSAGE_COMPLETE' " & _
      " OR MessageStatus='MESSAGE_PENDING' " & _
      " OR MessageStatus='MESSAGE_FORWARD' " & _
      " AND ContactDate >=DATEADD(day, -30, getdate()) "

      So Far I've got this

      Dim messages As IQueryable(Of CRM_MESSAGES)
      messages = messages.Where(Function(m) m.MessageStatus = "MESSAGE_COMPLETE")
      messages = messages.Where(Function(m) m.MessageStatus = "MESSAGE_PENDING")
      messages = messages.Where(Function(m) m.MessageStatus = "MESSAGE_FORWARD")
      messages = messages.Where(Function(m) m.ContactDate >= m.ContactDate.Date.AddDays(-30))

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Multiple Where statements is equivalent to an AND operator. For an Or operator, use VB's OrElse operator:

      messages = messages.Where(Function(m) m.MessageStatus = "MESSAGE_COMPLETE" OrElse m.MessageStatus = "MESSAGE_PENDING" OrElse m.MessageStatus = "MESSAGE_FORWARD")

      Your date comparison won't do anything - you're asking for all messages where the contact date is later than a date 30 days before the contact date, which is all of them. Use Date.Today.AddDays(-30) instead:

      messages = messages.Where(Function(m) m.ContactDate >= Date.Today.AddDays(-30))


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      J 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Multiple Where statements is equivalent to an AND operator. For an Or operator, use VB's OrElse operator:

        messages = messages.Where(Function(m) m.MessageStatus = "MESSAGE_COMPLETE" OrElse m.MessageStatus = "MESSAGE_PENDING" OrElse m.MessageStatus = "MESSAGE_FORWARD")

        Your date comparison won't do anything - you're asking for all messages where the contact date is later than a date 30 days before the contact date, which is all of them. Use Date.Today.AddDays(-30) instead:

        messages = messages.Where(Function(m) m.ContactDate >= Date.Today.AddDays(-30))


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        J Offline
        J Offline
        jkirkerx
        wrote on last edited by
        #3

        I would of never figured that out on the multiple where statements. And the Date bombed, so I tried DateTime.Now.AddDays(-30), but changed to your example before testing it. I wonder if this will now work without using IQueryable? I'll give it a spin. It didn't work LINQ to Entities does not recognize the method 'System.DateTime AddDays(Double)' method, and this method cannot be translated into a store expression. I'll go back to IQueryable and give it a try.

        Dim pValue As Integer = 0
        Using context As New CRMContext()

                pValue = \_
                    (From item In context.CRM\_MESSAGES
                     Where item.MessageStatus = "MESSAGE\_COMPLETE" \_
                     OrElse item.MessageStatus = "MESSAGE\_PENDING" \_
                     OrElse item.MessageStatus = "MESSAGE\_FORWARD" \_
                     And item.ContactDate >= Date.Today.AddDays(-30)).Count()
        
            End Using
        
            Return pValue
        
        J Richard DeemingR 2 Replies Last reply
        0
        • J jkirkerx

          I would of never figured that out on the multiple where statements. And the Date bombed, so I tried DateTime.Now.AddDays(-30), but changed to your example before testing it. I wonder if this will now work without using IQueryable? I'll give it a spin. It didn't work LINQ to Entities does not recognize the method 'System.DateTime AddDays(Double)' method, and this method cannot be translated into a store expression. I'll go back to IQueryable and give it a try.

          Dim pValue As Integer = 0
          Using context As New CRMContext()

                  pValue = \_
                      (From item In context.CRM\_MESSAGES
                       Where item.MessageStatus = "MESSAGE\_COMPLETE" \_
                       OrElse item.MessageStatus = "MESSAGE\_PENDING" \_
                       OrElse item.MessageStatus = "MESSAGE\_FORWARD" \_
                       And item.ContactDate >= Date.Today.AddDays(-30)).Count()
          
              End Using
          
              Return pValue
          
          J Offline
          J Offline
          jkirkerx
          wrote on last edited by
          #4

          DBFunctions for EF 6.0, Thanks!

          Imports System.Data.Entity

          Dim pValue As Integer = 0

              Using context As New CRMContext()
          
                  pValue = \_
                      (From item In context.CRM\_MESSAGES
                       Where item.MessageStatus = "MESSAGE\_COMPLETE" \_
                       OrElse item.MessageStatus = "MESSAGE\_PENDING" \_
                       OrElse item.MessageStatus = "MESSAGE\_FORWARD" \_
                       And item.ContactDate >= DbFunctions.AddDays(DateTime.Now(), -30)).Count()
          
              End Using
          
              Return pValue
          
          1 Reply Last reply
          0
          • J jkirkerx

            I would of never figured that out on the multiple where statements. And the Date bombed, so I tried DateTime.Now.AddDays(-30), but changed to your example before testing it. I wonder if this will now work without using IQueryable? I'll give it a spin. It didn't work LINQ to Entities does not recognize the method 'System.DateTime AddDays(Double)' method, and this method cannot be translated into a store expression. I'll go back to IQueryable and give it a try.

            Dim pValue As Integer = 0
            Using context As New CRMContext()

                    pValue = \_
                        (From item In context.CRM\_MESSAGES
                         Where item.MessageStatus = "MESSAGE\_COMPLETE" \_
                         OrElse item.MessageStatus = "MESSAGE\_PENDING" \_
                         OrElse item.MessageStatus = "MESSAGE\_FORWARD" \_
                         And item.ContactDate >= Date.Today.AddDays(-30)).Count()
            
                End Using
            
                Return pValue
            
            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            jkirkerx wrote:

            LINQ to Entities does not recognize the method 'System.DateTime AddDays(Double)' method, and this method cannot be translated into a store expression.

            You might need to move the date outside of the filter:

            Dim minDate As Date = Date.Today.AddDays(-30)
            messages = messages.Where(Function(m) m.ContactDate >= minDate)

            Alternatively, use the EntityFramework.SqlServer assembly, and the System.Data.Entity.SqlServer.SqlFunctions class[^]:

            messages = messages.Where(Function(m) m.ContactDate >= SqlFunctions.DateAdd("day", -30, SqlFunctions.GetDate()))


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            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