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

前端 未结 7 633
灰色年华
灰色年华 2020-12-07 20:27

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

static string GetLongestStringInList()
{
    string long         


        
7条回答
  •  庸人自扰
    2020-12-07 21:04

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

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

    The variable maxString will contain the value "HELLO WORLD"

提交回复
热议问题