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. Visual Basic
  4. help with linq query

help with linq query

Scheduled Pinned Locked Moved Visual Basic
helpcsharpdatabaselinqtutorial
5 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.
  • B Offline
    B Offline
    byka
    wrote on last edited by
    #1

    I am trying to use linq...however running into the issue... how to I combine 3 fileds to Date?

    .Dates = rows.Select(Function(r) r.Field(Of DateTime?)("DEIBMT") & "/" & ("DEIBDY") & "/" & ("DEIBCY") & ("DEIBYR"))

    Dim data = myDataTable.AsEnumerable().GroupBy(Function(r) New With {Key .EEGRP = r.Field(Of String)("EEGRP"), Key .EESSN = r.Field(Of String)("EESSN")},
    Function(key, rows) New With
    {
    Key .EEGRP = key.EEGRP,
    Key .EESSN = key.EESSN,
    .Dates = rows.Select(Function(r) _
    r.Field(Of String)("DEIBMT")) _
    .OrderByDescending(Function(d) d).Take(3).ToList()}
    )
    ' ' .Dates = rows.Select(Function(r) r.Field(Of DateTime?)("DEIBMT") & "/" & ("DEIBDY") & "/" & ("DEIBCY") & ("DEIBYR")) _

    Richard DeemingR 1 Reply Last reply
    0
    • B byka

      I am trying to use linq...however running into the issue... how to I combine 3 fileds to Date?

      .Dates = rows.Select(Function(r) r.Field(Of DateTime?)("DEIBMT") & "/" & ("DEIBDY") & "/" & ("DEIBCY") & ("DEIBYR"))

      Dim data = myDataTable.AsEnumerable().GroupBy(Function(r) New With {Key .EEGRP = r.Field(Of String)("EEGRP"), Key .EESSN = r.Field(Of String)("EESSN")},
      Function(key, rows) New With
      {
      Key .EEGRP = key.EEGRP,
      Key .EESSN = key.EESSN,
      .Dates = rows.Select(Function(r) _
      r.Field(Of String)("DEIBMT")) _
      .OrderByDescending(Function(d) d).Take(3).ToList()}
      )
      ' ' .Dates = rows.Select(Function(r) r.Field(Of DateTime?)("DEIBMT") & "/" & ("DEIBDY") & "/" & ("DEIBCY") & ("DEIBYR")) _

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

      It depends what type your columns are. Assuming integers, something like this should work:

      .Dates = rows.Select(Function(r) New DateTime( _
      (100 * r.Field(Of Integer)("DEIBCY")) + r.Field(Of Integer)("DEIBYR"), _
      r.Field(Of Integer)("DEIBMT"), _
      r.Field(Of Integer)("DEIBDY")))


      "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

      B 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        It depends what type your columns are. Assuming integers, something like this should work:

        .Dates = rows.Select(Function(r) New DateTime( _
        (100 * r.Field(Of Integer)("DEIBCY")) + r.Field(Of Integer)("DEIBYR"), _
        r.Field(Of Integer)("DEIBMT"), _
        r.Field(Of Integer)("DEIBDY")))


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

        B Offline
        B Offline
        byka
        wrote on last edited by
        #3

        All columns string... could you show me how I should do this with strings?

        Richard DeemingR 1 Reply Last reply
        0
        • B byka

          All columns string... could you show me how I should do this with strings?

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

          String representations of integers, or something else? If it's integers, then Integer.TryParse will work:

          Private Shared Function DateFromStringParts(ByVal century As String, ByVal year As String, ByVal month As String, ByVal day As String) As DateTime?
          Dim cy, yr, mt, dy As Integer
          If Not Integer.TryParse(century, cy) Then Return Nothing
          If Not Integer.TryParse(year, yr) Then Return Nothing
          If Not Integer.TryParse(month, mt) Then Return Nothing
          If Not Integer.TryParse(day, dy) Then Return Nothing
          Return New DateTime((100 * cy) + yr, mt, dy)
          End Function
          ...
          .Dates = rows.Select(Function(r) DateFromStringParts( _
          r.Field(Of String)("DEIBCY"), _
          r.Field(Of String)("DEIBYR"), _
          r.Field(Of String)("DEIBMT"), _
          r.Field(Of String)("DEIBDY")))


          "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

          G 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            String representations of integers, or something else? If it's integers, then Integer.TryParse will work:

            Private Shared Function DateFromStringParts(ByVal century As String, ByVal year As String, ByVal month As String, ByVal day As String) As DateTime?
            Dim cy, yr, mt, dy As Integer
            If Not Integer.TryParse(century, cy) Then Return Nothing
            If Not Integer.TryParse(year, yr) Then Return Nothing
            If Not Integer.TryParse(month, mt) Then Return Nothing
            If Not Integer.TryParse(day, dy) Then Return Nothing
            Return New DateTime((100 * cy) + yr, mt, dy)
            End Function
            ...
            .Dates = rows.Select(Function(r) DateFromStringParts( _
            r.Field(Of String)("DEIBCY"), _
            r.Field(Of String)("DEIBYR"), _
            r.Field(Of String)("DEIBMT"), _
            r.Field(Of String)("DEIBDY")))


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

            G Offline
            G Offline
            Garth J Lancaster
            wrote on last edited by
            #5

            nice !! :-)

            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