Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How get the biggest number?
i am trying to get the biggest number from a field in my database but is it strange because i use this simple code

C#
amenitiesExcel = dc.Hotel_Meals_TBLs.Select(Convert.ToInt32(a => a.CodeNumber)).Max();

and when i compile the query i receive this error:
Error   370 The type arguments for method 'System.Linq.Queryable.Select<TSource,TResult>(System.Linq.IQueryable<TSource>, System.Linq.Expressions.Expression<System.Func<TSource,TResult>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

...the field "CodeNumber" in the database is a Nvarchar Datatype then is filled only with number and i need to store the biggest number in this field ,do you have any idea or suggestion how i can get the biggest number in this case

Thanks so much for the support.

Cheers
Posted
Updated 7-May-13 7:41am
v2

You should try this:
C#
amenitiesExcel = dc.Hotel_Meals_TBLs.Max(a => Convert.ToInt32(a.CodeNumber));

or
C#
amenitiesExcel = dc.Hotel_Meals_TBLs.Max(a => int.Parse(a.CodeNumber));
 
Share this answer
 
You don't need to Convert to int. Just change your Query like below
C#
amenitiesExcel = dc.Hotel_Meals_TBLs.Max(a => a.CodeNumber);

Hope this helps
 
Share this answer
 
Comments
VisualLive 7-May-13 3:27am    
@James Moideen Thanks fof the reply i tried your suggestion but i received this error Error 369 Cannot implicitly convert type 'string' to 'Software2011.Room' ,sincerely if i dont convert it the MAX number that store is '999' instead in the field CodeNumber the MAX number is 1671 .
Jameel VM 7-May-13 5:26am    
In which type you tried to return the linq query results?
Matt T Heffron 8-May-13 16:39pm    
OP said the CodeNumber is Nvarchar, so using Max like this gives the lexicographically largest string "999".
Try this
C#
int amenitiesExcel = 0;
amenitiesExcel = dc.Hotel_Meals_TBLs.Max(a => (int?)a.CodeNumber)?? 0;
 
Share this answer
 
Comments
Matt T Heffron 8-May-13 16:42pm    
OP said the CodeNumber is Nvarchar, so using Max like this will probably throw an exception on the cast.
C#
int TExcel = 0;
TExcel = dc.Hotel_Meals_TBLs.Max(a => a.CodeNumber);


It is allready converted!
 
Share this answer
 
Comments
Matt T Heffron 8-May-13 16:40pm    
OP said the CodeNumber is Nvarchar, so using Max like this gives the lexicographically largest string "999".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900