How to find max value in a column in a datarow[] ?

Shay Krainer picture Shay Krainer · Jul 30, 2011 · Viewed 9.9k times · Source

I have a simple problem but I just don't understand any of the examples I find here or in MSDN. (I'm still new to C# and all the datasets functions).

I have a datatable "tblRoom" which its columms are "building","apartment" and "room" and they're all ints and the table's primary keys (it's a weak entity of apartment (which is weak entity of building) without other properties/columns).

I also have DataRow[] roomCollection that selects a specific apartment in a building with this code:

roomCollection = dbDataSet.tblRoom.Select("building ='"+ b_int + 
                                          "' and apartment='"+ a_int + "'");

All this runs fine (I guess...). Now I want to get max value of room from this apartment (the max room number in this apartment). I've tried to no avail these codes:

DataRow dr = roomCollection.Max();
int maxi = roomCollection.Max();

I just don't get from the tooltip what am I suppose to write in the function. It throws exceptions on either no IEnumerable or Icomparable.

What do I need to write to get the max value (int) in the room column? Anyone knows a "[something] for dummies" that explains it to an idiot because I don't understand the meaning of the errors/tooltip of what am I suppose to write in the Max().

EDIT: The tooltip suggest to enter these (shown relevants):

(this IEnumerable <DataRow> source):DataRow
(this IEnumerable <DataRow> source, Func<DataRow,int?> selector):int?
(this IEnumerable <DataRow> source, Func<DataRow,int> selector):int

I really don't get it :(

Thanks ahead, Shay.

Answer

Yahia picture Yahia · Jul 30, 2011

try

int roomno = roomCollection.Max (r => (int) r["room"]);