开发者

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

开发者 https://www.devze.com 2023-03-23 20:23 出处:网络
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 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().开发者_如何学Python

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.


try

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


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"]);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号