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

This will do it with only one loop iteration:

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

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


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

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:

Linq group by with a sub query


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.