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();
4
  • where is your where statement? where is your Covnert.ToInt32? Commented Dec 5, 2013 at 6:11
  • Do you have values in TopicId ? what is the data type of TopicId ? Commented Dec 5, 2013 at 6:12
  • yep! 100, 200, 300, 400, and 500 Commented Dec 5, 2013 at 6:12
  • with the where statement it read var maxTopID = (from max in dbcontext.Topics.Local where Convert.ToInt32(max.TopicID) select max.TopicID).Max(); Commented Dec 5, 2013 at 6:13

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
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.
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

Comments

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.