Is there a LINQ function for getting the longest string in a list of strings?

C#StringLinq

C# Problem Overview


Is there a LINQ function for this is or would one have to code it themselves like this:

static string GetLongestStringInList()
{
    string longest = list[0];

    foreach (string s in list)
    {
        if (s.Length > longest.Length)
        {
            longest = s;
        }
    }

    return longest;
}

C# Solutions


Solution 1 - C#

This will do it with only one loop iteration:

list.Aggregate("", (max, cur) => max.Length > cur.Length ? max : cur);

Solution 2 - C#

You can use this: list.OrderByDescending(s => s.Length).First();

Solution 3 - C#

var list = new List<string>(); // or string[] or any

list.Add("a");
list.Add("ccc");
list.Add("bb");
list.Add("eeeee");
list.Add("dddd");

// max-length
var length = list.Max(s => s.Length);

// biggest one
var biggest = list.FirstOrDefault(s => s.Length == length);

// if there is more that one by equal length
var biggestList = list.Where(s => s.Length == length);

// by ordering list
var biggest = list.OrderByDescending(s => s.Length).FirstOrDefault();

// biggest-list by LINQ
var bigList2 = from s in list where s.Length == list.Max(a => a.Length) select s;

// biggest by LINQ
var biggest2 = bigList2.FirstOrDefault();

Solution 4 - C#

The method you want is typically called "MaxBy" and it is unfortunately not included in the standard set of sequence operators. Fortunately it is very easy to write yourself. See this answer for an implementation:

https://stackoverflow.com/questions/976808/linq-group-by-with-a-sub-query/976849#976849

Solution 5 - C#

I thought there was a better way to get the longest string in a list of strings using the lambda expressions. One such way is as below.

string longestString = list.Max(arr => arr);

Hope this works good for the answer seekers.

Solution 6 - C#

To get the longest string in list of object/string try this:

List<String> list = new List<String>();
list.Add("HELLO");
list.Add("HELLO WORLD");
String maxString = list.OrderByDescending(x => x.Length).First();

The variable maxString will contain the value "HELLO WORLD"

Solution 7 - C#

Add a ThenBy() to guarantee a return order if there are multiple strings with the same length

var longest = list.OrderByDescending(s => s.Length)
                   .ThenBy(s => s)
                   .FirstOrDefault();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1022677View Question on Stackoverflow
Solution 1 - C#SimonCView Answer on Stackoverflow
Solution 2 - C#DaniView Answer on Stackoverflow
Solution 3 - C#amiry jdView Answer on Stackoverflow
Solution 4 - C#Eric LippertView Answer on Stackoverflow
Solution 5 - C#Sudharshan KalaleView Answer on Stackoverflow
Solution 6 - C#daniele3004View Answer on Stackoverflow
Solution 7 - C#cordialgermView Answer on Stackoverflow