How will you obtain the length of the longest string in a Data table assuming that every column in of type string using Lambda?

 Posted by Niladri.Biswas on 7/18/2012 | Category: LINQ Interview questions | Views: 4806 | Points: 40

DataTable dt = new DataTable();

var maxLength = dt.AsEnumerable()
.SelectMany(r => r.ItemArray.OfType<string>())
.Max(i => i.Length);

First cast the Datatable to System.Collections.Generic.IEnumerable<T> object by using the AsEnumerable() extension method,then using the SelectMany extension method
to find out the items in the ItemArray whose type is string and finally figuring out the max length of the records.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response