2

This is what I have and it keeps returning null.

It doesn't recognize the Convert.toInt32 when I add a where statement

var maxTopID = (from max in dbcontext.Topics.Local
               select max.TopicID).Max();
Dragos Durlut
  • 8,018
  • 10
  • 47
  • 62

3 Answers3

4

How about converting the TopicID in SELECT and use String.IsNullOrEmpty() to remove empty string, like:

 var maxTopID = (from max in dbcontext.Topics.Local
                 where !String.IsNullOrEmpty(max.TopicID)
                 select Convert.ToInt32(max.TopicID)).Max();

See the Demo

Edper
  • 9,144
  • 1
  • 27
  • 46
  • Does LINQ to SQL support `String.IsNullOrEmpty()`? I am pretty it doesn't support `String.IsNullOrWhiteSpace()`, but does it support the former one I am not sure. – Sнаđошƒаӽ Nov 19 '17 at 10:46
  • Did you check the **Demo** link? That's straight from a C# compiler. Also `String.IsNullOrEmpty()` could be use anywhere as long as you're testing a value if it is `null` or empty. – Edper Nov 19 '17 at 14:05
  • I visited that link, but didn't bother to run it, because that has nothing to do with my question. To be explicit, that doesn't involve LINQ to SQL. Nevermind, I will check myself tomorrow, and let you know, if I remember that is. After all, the OP isn't about LINQ to SQL itself. – Sнаđошƒаӽ Nov 19 '17 at 17:51
1

Check for the null condition as mentioned in below query

var maxTopID = (from max in dbcontext.Topics.Local
                 where  max.TopicId != null
                 select max.TopicID).Max();
SpiderCode
  • 10,062
  • 2
  • 22
  • 42
1

I think you are saying that TopicID is string and you want to convert it to int

var list= (from max in dbcontext.Topics.Local
                     where  max.TopicId != null
                     select max.TopicID).ToList();

int max=0;

if (list.Count() !=0)
max=list.Select(int.Parse).ToList().Max();

max will contain max value from list which is converted to list of integer

Nitin Varpe
  • 10,450
  • 6
  • 36
  • 60