-1

I have this list:

public static List<PhraseModel> phraseList;

Where the PhraseModel class looks like this:

public class PhraseModel
{
    public string PhraseId { get; set; }
    public string English { get; set; }
    public string PhraseNum { get; set; }

}

How can I find the maximum value of PhraseNum using LINQ

apologies to those that made the comments. PhraseNum is always an int number but in a string field. It was set to string because of the way it's read from Excel

Alan2
  • 23,493
  • 79
  • 256
  • 450
  • how do you know phraseNum will be a number or a decimal? he has declared it as a string – Baahubali Sep 27 '19 at 08:20
  • Please, provide some *examples*. What is `PhraseNum`, if it's an integer value, why it's of type `string`? – Dmitry Bychenko Sep 27 '19 at 08:22
  • Do you mean "maximum value" in terms of alphabetical order, or is `PhraseNum` going to always be a number? Or is `PhraseNum` a mixture of alphanumeric characters that you want to sort in a "natural order", that is, A2 comes BEFORE A10? – Matthew Watson Sep 27 '19 at 08:23
  • How to find max from string? do you want to get based on `string with max length`? or `PhraseNum` is a numeric value? if it is an numeric value then provide change data type of that property or provide us some input data – Prasad Telkikar Sep 27 '19 at 08:23
  • Another question: Do you simply want the maximum value of `PhraseNum`? You don't need the associated `PhraseModel` object that contains the maximum? – Matthew Watson Sep 27 '19 at 08:34
  • Possible duplicate of [How do I get the max ID with Linq to Entity?](https://stackoverflow.com/questions/315946/how-do-i-get-the-max-id-with-linq-to-entity) – NetCoreDev Sep 27 '19 at 14:10

4 Answers4

6

Linq has a Max extension method. Try like:

phraseList.Max(x=>int.Parse(x.PhraseNum));
Jota.Toledo
  • 27,293
  • 11
  • 59
  • 73
apomene
  • 14,282
  • 9
  • 46
  • 72
5

You can use .Max() from Linq. Here you don't need Select()

int result = phraseList.Max(x => int.Parse(x.PhraseNum));
Console.WriteLine(result); //max PhraseNum from list

To avoid exception you can use int.TryParse() mentioned by @haldo

Like,

//This is C# 7 Out variable feature. 
int result = phraseList.Max(p => int.TryParse(p.PhraseNum, out int phraseNumInt) ? phraseNumInt: int.MinValue);
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
2

You can use TryParse instead of Parse to avoid exception if there's a chance PhraseNum is not an int.

int tmp;
var max = phraseList.Max(p => int.TryParse(p.PhraseNum, out tmp) ? tmp : int.MinValue);

If you're using C# 7.0 or above you can use the following syntax with inline variable declaration inside the TryParse:

var max = phraseList.Max(p => int.TryParse(p.PhraseNum, out int tmp) ? tmp : int.MinValue);

Or without the TryParse:

var max = phraseList.Max(p => int.Parse(p.PhraseNum));
haldo
  • 14,512
  • 5
  • 46
  • 52
1

This Code Return 8

PhraseModel phraseModel = new PhraseModel() { PhraseNum = "5" };
phraseList.Add(phraseModel);
PhraseModel phraseModel2 = new PhraseModel() { PhraseNum = "3" };
phraseList.Add(phraseModel2);
PhraseModel phraseModel3 = new PhraseModel() { PhraseNum = "4" };
phraseList.Add(phraseModel3);
PhraseModel phraseModel4 = new PhraseModel() { PhraseNum = "8" };
phraseList.Add(phraseModel4);

int number;

var maxPhraseNumber = phraseList.Select(n => int.TryParse(n.PhraseNum, out number) ? number : 0).Max();