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 query corresponding to LINQ

Sql query corresponding to LINQ

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinq
4 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.
  • N Offline
    N Offline
    Nilish
    wrote on last edited by
    #1

    I have the following table id Name 1 A 2 A 3 A 4 B 5 B 6 C in sql select name , max(id)as Id from tablename groupby name having count(name) >1 it provides us the following result id Name 3 A 5 B How Should i figure out the same in LINQ VB.net 3.5

    H 1 Reply Last reply
    0
    • N Nilish

      I have the following table id Name 1 A 2 A 3 A 4 B 5 B 6 C in sql select name , max(id)as Id from tablename groupby name having count(name) >1 it provides us the following result id Name 3 A 5 B How Should i figure out the same in LINQ VB.net 3.5

      H Offline
      H Offline
      Howard Richards
      wrote on last edited by
      #2

      In VB this is

      dim query = From T In db.Table _
      Group T by Name = T.Name _
      Into c=Count(), MaxID=Max(T.ID) _
      Where c > 1 _
      Select MaxID, Name

      'Howard

      H 1 Reply Last reply
      0
      • H Howard Richards

        In VB this is

        dim query = From T In db.Table _
        Group T by Name = T.Name _
        Into c=Count(), MaxID=Max(T.ID) _
        Where c > 1 _
        Select MaxID, Name

        'Howard

        H Offline
        H Offline
        himenderc
        wrote on last edited by
        #3

        Hi Harward thanxs for reply. it give me following message when i using MaxID=Max(T.ID) _ Error Message. Overload resolution failed because no accessible 'Max' can be called without a narrowing conversion: Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Decimal?)) As Decimal?' defined in 'System.Linq.Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Decimal?'. Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Decimal)) As Decimal' defined in 'System.Linq.Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Decimal'. Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Double?)) As Double?' defined in 'System.Linq.Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Double?'. Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Double)) As Double' defined in 'System.Linq.Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Double'. Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Single?)) As Single?' defined in 'System.Linq.Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Single?'. Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Single)) As Single' defined in 'System.Linq.Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Single'. Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Long?)) As Long?' defined in 'System.Linq.Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Long?'. Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Long)) As Long' defined in 'System.Linq.Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Long'. Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Integer?)) As Integer?' defined in 'System.Linq.Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Integer?'. Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Integer))

        H 1 Reply Last reply
        0
        • H himenderc

          Hi Harward thanxs for reply. it give me following message when i using MaxID=Max(T.ID) _ Error Message. Overload resolution failed because no accessible 'Max' can be called without a narrowing conversion: Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Decimal?)) As Decimal?' defined in 'System.Linq.Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Decimal?'. Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Decimal)) As Decimal' defined in 'System.Linq.Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Decimal'. Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Double?)) As Double?' defined in 'System.Linq.Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Double?'. Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Double)) As Double' defined in 'System.Linq.Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Double'. Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Single?)) As Single?' defined in 'System.Linq.Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Single?'. Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Single)) As Single' defined in 'System.Linq.Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Single'. Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Long?)) As Long?' defined in 'System.Linq.Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Long?'. Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Long)) As Long' defined in 'System.Linq.Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Long'. Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Integer?)) As Integer?' defined in 'System.Linq.Enumerable': Return type of nested function matching parameter 'selector' narrows from 'Object' to 'Integer?'. Extension method 'Public Function Max(selector As System.Func(Of <anonymous type>, Integer))

          H Offline
          H Offline
          Howard Richards
          wrote on last edited by
          #4

          I assumed that ID was not a nullable value (if it truly is an ID it should not be). LINQ is saying it's not sure which extension method to use with ID since it's not an Int but Int? (nullable int). If you don't want to include the nulls, just cast it to an int:

          dim query = From T In db.Table _
          Group T by Name = T.Name _
          Into c=Count(), MaxID=Max(Ctype(T.ID,Integer)) _
          Where c > 1 _
          Select MaxID, Name

          Or you can convert the nulls to a zero, e.g. MaxID=Max(T.ID.GetValueOrDefault(0))

          'Howard

          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