Share via

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

Developer technologies | VB
0 comments No comments

Answer accepted by question author

Viorel 127K 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))

Was this answer helpful?


1 additional answer

Sort by: Most helpful
  1. Viorel 127K 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?

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.