首页 \ 问答 \ 如何在datarow []中的列中找到最大值?(How to find max value in a column in a datarow[] ?)

如何在datarow []中的列中找到最大值?(How to find max value in a column in a datarow[] ?)

我有一个简单的问题,但我只是不明白我在这里或在MSDN中找到的任何示例。 (我还是C#和所有数据集功能的新手)。

我有一个数据表“tblRoom”,它的columms是“建筑”,“公寓”和“房间”,它们都是整体和桌子的主要钥匙(它是公寓的弱实体(建筑物的弱实体)而没有其他属性/列)。

我还有DataRow[] roomCollection ,它使用以下代码选择建筑物中的特定公寓:

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

这一切都运行良好(我猜......)。 现在我希望从这间公寓获得房间的最大价值(这间公寓的最大房间数)。 我试图无效这些代码:

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

我只是没有从工具提示中得到我想在函数中写的内容。 它会抛出没有IEnumerable或Icomparable的异常。

为了获得房间栏中的最大值(int),我需要写什么? 任何人都知道一个“傻瓜的东西”,它解释了它是一个白痴,因为我不明白我想在Max()写的错误/工具提示的含义。

编辑:工具提示建议输入这些(显示相关性):

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

我真的不明白:(

谢谢,谢谢。


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.


原文:https://stackoverflow.com/questions/6885169
更新时间:2020-05-29 23:40

最满意答案

尝试

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

try

int roomno = roomCollection.Max (r => (int) r["room"]);
2011-07-30

相关问答

更多

如何查看VB.Net DataRow中是否存在列(How do I find out if a column exists in a VB.Net DataRow)

DataRow是很好的方式,他们的底层表链接到他们。 使用基础表,您可以验证一个特定的行中有一个特定的列。 If DataRow.Table.Columns.Contains("column") Then MsgBox("YAY") End If DataRow's are nice in the way that they have their underlying table linked to them. With the underlying table ...

使用额外列复制DataRow(Copy a DataRow with an extra column)

这应该按照需要工作: foreach (DataRow dr in ret.Rows) { DataRow row = dt.Rows.Add(); for (int i = 0; i < dr.ItemArray.Length; i++) row.SetField(i, dr.ItemArray[i]); row.SetField(dt.Columns.Count - 1, "1"); } 另一种方法是将DefaultValue分配给最后一列,因为您始终 ...

从DataRow []获取具有最大DateTime值的DataRow(Getting the DataRow with max DateTime value from DataRow[])

这解决了我的问题: dr = potentialRows.OfType<DataRow>().OrderBy(r => r["date"]).LastOrDefault<DataRow>(); 按日期字段排序数组并获取保存最新日期的最后一个元素。 This solved my problem: dr = potentialRows.OfType<DataRow>().OrderBy(r => r["date"]).LastOrDefault<DataRow>(); ordering the a ...

如何使用逗号连接List 的特定列?(How to join a specific column of List with a comma?)

你忘row.ToString() row[1] ,如果你做row.ToString()输出的是类名DataRow string forms = string.Join(", ", rows.Select(row => row[1].ToString()).ToArray()); 如果您使用.NET> 4,则可以跳过.ToArray()因为存在IEnumerable重载 string forms = string.Join(", ", rows.Select(row => row[1].ToStr ...

通过datarow中的字段值获取列名称(Get column name by value of field in datarow)

当前代码,每行*每列 获取列的名称 将其存储在变量中 在匹配的情况下执行String.Replace 我的建议: var allCols = dt.Columns .Cast<DataColumn>() .Select(col => col.ColumnName.Replace("Apat", "")) .ToArray(); foreach (DataRow r in dt.Rows) { ...

如何在datarow []中的列中找到最大值?(How to find max value in a column in a datarow[] ?)

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

如何从绑定的DataRow中确定DataGridViewRow(How to determine a DataGridViewRow from the DataRow it is bound to)

在行的主键上调用dataView.Find 。 编辑 :要查找DataRowView的DataGridViewRow ,请调用grid.Rows[dataView.IndexOf(drv)) 。 Call dataView.Find on the row's primary key. EDIT: To find the DataGridViewRow for a DataRowView, call grid.Rows[dataView.IndexOf(drv)).

如何在PowerShell中比较两个DataRow对象数组?(How do I compare two arrays of DataRow objects in PowerShell?)

我写了一个脚本来做这件事一会儿。 脚本(Compare-QueryResults.ps1) 在这里可用,您还需要我的Run-SQLQuery脚本(可在此处获得 ),或者您可以使用您自己的脚本或函数替换它。 基本上,脚本所做的是获取每个查询的结果并将数据行分开,以便每个字段都是它自己的对象。 然后,它使用Compare-Object检查这些行中的数据之间的任何差异。 它返回一个比较对象,显示返回数据之间的所有差异。 结果是一个对象,因此您可以将它们保存到变量中,并使用Sort-Object或Form ...

相关文章

更多

最新问答

更多
  • 如何在laravel 5.3中获取参数?(How to get parameter in laravel 5.3?)
  • 使用带结构的地图作为键 - 值不保存[重复](Using map with structure as key - value doesn't save [duplicate])
  • GET请求,包含数据(GET request, with data)
  • 如何在javascript / NODE.js中从JSON获取第一个键?(How can I get the first key from JSON in javascript / NODE.js?)
  • 使用自己方法的链接进行索引分页(Index pagination with links to own method)
  • 仅使用密钥(GUID)从winform登录[关闭](Login from winform using only a key(GUID) [closed])
  • AngularJS - 重复在后台运行任务(AngularJS - run task in background repeatedly)
  • 使用Flask和JQuery,如何通过“性感”方式的“POST”方法删除记录?(With Flask and JQuery, how to delete a record by “POST” method in “sexy” way?)
  • 无法在DataGridView单元格BackColor中使用Color.FromArgb(Cannot use Color.FromArgb in DataGridView cell BackColor)
  • 将JavaScript变量值获取到Asp.net的Code Behind(get the javascript variable value to Code Behind Asp.net)
  • DMA工作时不会发生STM32F4 TIM6中断(STM32F4 TIM6 interruption doesn't happen while DMA working)
  • 关于带圆角的UIImageView(About UIImageView with round corners)
  • 如何运行我的代码[重复](How to run my Code [duplicate])
  • bash会对未设置的变量做什么(what will bash do with an unset variable)
  • ZF2 - 使用post redirect get并重定向到带有params的URL(ZF2 - Using post redirect get and redirecting to a URL with params)
  • Symfony EasyAdminBundle自定义视图(Symfony EasyAdminBundle custom view)
  • while循环中的子查询:什么是最快的?(Sub-queries in a while loop: what is fastest?)
  • Magento - 忘记密码链接生成使用不正确的商店视图(Magento - Forgotten password link generation is using incorrect store view)
  • 使用32位DLL找不到入口点。(Entry point not found with 32bits DLL. Working with 64 bits DLL)
  • 一个col中的变量;(Variables in one col; values in another->goal: add columns for variables)
  • 在另一个目录中执行文件[重复](Execute file in another directory [duplicate])
  • 我希望我的View能够识别另一个View的类(I want my View recognize the class of another View)
  • 没有for循环的验证消息访问(Validation message acess without for loop)
  • Mozilla中的Bootstrap 3模态没有显示(Bootstrap 3 modal in mozilla not showing)
  • 使用R中的日期时间变量处理数据帧(Manipulating dataframe with date time variable in R)
  • JQuery UI datepicker在异步调用,多个问题上的行为(Behaviour of JQuery UI datepicker on asynchronous call, multiple issues)
  • 如何在JavaScript对象中添加属性?(How to add a property in JavaScript object? [duplicate])
  • 有关.mdf数据库文件类型的问题(Questions regarding .mdf database filetype)
  • Qt Designer或关联的Qt工具是否具有xml格式的* .ui文件的全局查找和替换功能?(Does Qt Designer or associated Qt tool have a global find-and-replace functionality for the xml formatted *.ui files?)
  • AWS多区域灾难恢复和负载平衡 - 最佳方法?(AWS multi-zone distaster recovery and load balancing - best approach?)