0

If I implement the following:

var maxValue = dataTable.Compute("Max(columnName)");

then presumably the type of maxValue will be the datatype of the 'columnName' column in dataTable (or will it??). But what comparer does the Max expression use, and what dataTypes are supported? If the values in the column cannot be compared then Max cannot be evaluated and presumably an exception will be thrown. Is it sufficient to ensure that the column datatype implements IComparable, thus:

if (typeof(IComparable).IsAssignableFrom(dataTable.Columns[columnName].DataType))
Dave
  • 3,429
  • 2
  • 26
  • 29
  • I think you missed the point. If the datatype of the columnName column is MyClass, what will happen? What if MyClass inherits from IComparable? – Dave Feb 13 '19 at 18:27
  • @Hans This is not the case. [See DataColumn.Add(string, type)](https://learn.microsoft.com/en-us/dotnet/api/system.data.datacolumncollection.add?view=netframework-4.7.2#System_Data_DataColumnCollection_Add_System_String_System_Type_). In fact I am already adding columns of type MyClass quite successfully - the only constraint is that they must implement Serializable. However, if you prefer, suppose the type of the column is string - what string comparator would be used (I realise that finding Max(string) is a bit meaningless, but it illustrates the point.) – Dave Feb 14 '19 at 09:26
  • https://referencesource.microsoft.com/#System.Data/System/Data/Common/SQLTypes/SQLStringStorage.cs,53 – Hans Passant Feb 14 '19 at 12:34

0 Answers0