Linq Query

Peter Newman 66 Reputation points
2021-11-16T19:05:02.74+00:00

Dim NewSerial = (From c In db.new_oaccounts Where Not String.IsNullOrEmpty(c.new_accountserial) Select Convert.ToInt32(c.new_accountserial)).Max

Im trying to get the max value for new_accountserial which is a string (0000xx) field . Ideally I would like to use a where clause as well i.e. where field = string vale

for instance if new_oaccounts has the following records

Name new_accountSerial
John 000004
Fred 000001
John 000002

if I queried 'John' i want to get the number 4 back

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
1,834 questions
No comments
{count} votes

Accepted answer
  1. Viorel 82,906 Reputation points
    2021-11-16T19:36:47.99+00:00

    If LINQ still is not able to recognise Convert.ToInt32, then try an alternative:

    Dim NewSerial = (From c In db.new_oaccounts
                        Where Not String.IsNullOrEmpty(c.new_accountSerial) AndAlso c.Name = "John"
                        Select c.new_accountSerial).ToList.Max(Function(s) Convert.ToInt32(s))
    

1 additional answer

Sort by: Most helpful
  1. Viorel 82,906 Reputation points
    2021-11-16T19:22:16.04+00:00

    Did you already try the next statement in your researches?

    Dim NewSerial = (From c In db.new_oaccounts
                        Where Not String.IsNullOrEmpty(c.new_accountSerial) AndAlso c.Name = "John"
                        Select Convert.ToInt32(c.new_accountSerial)).Max
    

    What errors did you get?