Sql query corresponding to LINQ
-
-
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
-
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))
-
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))
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
butInt?
(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, NameOr you can convert the nulls to a zero, e.g.
MaxID=Max(T.ID.GetValueOrDefault(0))
'Howard