Find longest string in Datatable column

前端 未结 3 867
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-14 06:05

I would like to know If It\'s possible to create a \"one-line\" Linq to retrieve longest string value of specific Datatable column, meaning that all column data (numbers, dates,

3条回答
  •  面向向阳花
    2021-02-14 06:29

    You are looking for ArgMax - a value such that it has max value of some kind of property. Standard Linq doesn't provide ArgMax but you can implement it via Aggregate (i.e. get a single value from a sequence):

     string maxString = dt
       .AsEnumerable()
       .Select(row => row[mycolumn].ToString())
       .Aggregate((s, a) => a.Length > s.Length ? a : s);
    

提交回复
热议问题