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; }
In this, we use inbuilt max() with “len” as key argument to extract the string with the maximum length.
Use Python's built-in max() function with a key argument to find the longest string in a list. Call max(lst, key=len) to return the longest string in lst using the built-in len() function to associate the weight of each string—the longest string will be the maximum.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With