3

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.

Shay Krainer
  • 35
  • 1
  • 1
  • 5

3 Answers3

3

try

int roomno = roomCollection.Max (r => (int) r["room"]);
Yahia
  • 69,653
  • 9
  • 115
  • 144
  • Ah it works! Thanks a bunch. I just have 2 more little questions: 1. Is that syntax LINQ? 2. What does it mean? var "r" gets (casted int) from ["room"]? I really don't understand how to pass this knowledge to other problems... why "r" is the left operand of => and on the right side of it too near the column name? – Shay Krainer Jul 30 '11 at 18:55
  • 1
    the syntax is linq with delegates... the r is the first parameter of the delegate and is of type DataRow (since it comes from DataRowCollection rommCollection) and after => that is the body of the delegate representing that value used by the Max extension method to evaluate... – Yahia Jul 30 '11 at 19:04
  • @Yahia . And if I want to find max value of string column? thanks. –  Jun 05 '19 at 10:53
1

In this case you would want to pass the selector to Max() to properly identify the column you want to max..

int max = roomCollection.Max(dr => (int)dr["room"]);
Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
1

Since roomCollection is an array of DataRows, DataRow dr = roomCollection.Max(); means select the maximum DataRow from roomCollection. But what does it mean for a DataRow to be greater than another DataRow? In general there isn't any useful comparison which is why .Max() fails on you.

Technically DataRow doesn't implement IComparable because there is on generic useful why to compare two DataRow's and tell which one is 'larger' or 'smaller'. However for integers there is since 2 is larger than 1.

What you need to do is pass a selector function to Max() to tell it what maximum value you are looking for. So as the other answers indication you need to use:

int max = roomCollection.Max(dr => (int)dr["room"]);

This says for each DataRow select the integer value for room and get the maximum room value.

The .Max() function is a part of the Linq (Language Integration Query) added in .NET 3.5. So you need to read up on that to better understand what's going on. I've found 101 Linq Samples to be useful since it shows examples for almost every thing you would want to do.

shf301
  • 31,086
  • 2
  • 52
  • 86
  • @ShayKrainer. And if I want to find max value of string column? thanks. –  Jun 05 '19 at 10:50