返回所有行的数据表最大列宽。 [英] Return the datatable max column width over all rows.

查看:59
本文介绍了返回所有行的数据表最大列宽。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sqlclient方法执行此SQL语句:

  select   top   10  * 
来自 wlo_104466_CustomerPriceFile
订单 [UPC代码]

这似乎工作正常。



但是,当我使用以下代码获得每列的最大宽度时:

 List< int> ; maximumLengthForColumns = 
Enumerable.Range( 0 ,dataTable.Columns.Count)
.Select(col = > dataTable.AsEnumerable()
.Select(row = > row [col])。OfType< string>()
.Max(val = > val.Length))。ToList();

我得到一个异常:序列包含no元素。



但是,我可以在查询的相同数据结果上运行以下代码:

  foreach (DataRow row  in  dataTable.Rows)
{
foreach var item in row.ItemArray)
{
Console.Write(item + );
}
Console.WriteLine();
}

我可以写每一行,逐字段。



下一个查询,不包括前10名限制,但不是同一个表:

 选择 * 
来自 wlo_104466_CustomerPriceFile
订单 [UPC代码]

完美生成maximumLengthForColumns列表。



这是我在Microsoft SQL Server Management Studio中运行第一个查询时生成的数据:

 UPC Code Supplier代码部分无包装数量计量单位
---------------- ------------- --------- ---- --------------- ---------------
NULL HOB S228519T-P01 14 CTN
NULL LIN ED031252 4SRP CTN
NULL LIN ED031253 3.3SRP CTN
NULL LIN ED0312 67 10.3CT CTN
空LIN ED031269 11.9CT CTN
空LIN ED031271 10.3CT CTN
空LIN ED031288 3.7TB CTN
空LIN ED031289 4.6TB CTN
NULL LIN ED031290 3.5TB CTN
NULL LIN ED031291 3.7TB CTN

(10行受影响)

我喜欢使用Linq代码的干净外观并希望了解我做错了什么。



感谢您的帮助。



什么我试过了:



1)使用foreach循环迭代数据表,这似乎有用。

2)当通过maximumLengthForColumns例程进行跟踪,我注意到执行行似乎永远不会超过查询引发异常的.Select(row ...行),而是直到。查询中的Max(val ...行。



NEW

我尝试使用另一个表中包含所有空值的表栏:

<前郎=文字> C1 C2 C3 C4 C5
---------- ---------- --- ------- ---------- ----------
NULL 1C2 1C3 1C4 1C5
NULL 2C2 2C3 2C4 2C5
NULL 3C2 3C3 3C4 3C5
NULL 4C2 4C3 4C4 4C5
NULL 5C2 5C3 5C4 5C5
NULL 6C2 6C3 6C4 6C5
NULL 7C2 7C3 7C4 7C5
NULL 8C2 8C3 8C4 8C5
NULL 9C2 9C3 9C4 9C5



和最大宽度查询失败。



然后我添加了9第二列中包含所有空值的行:

 C2 C3 C4 C5 
---------- ------- --- ---------- ---- ------ ----------
NULL 1C2 1C3 1C4 1C5
NULL 2C2 2C3 2C4 2C5
NULL 3C2 3C3 3C4 3C5
NULL 4C2 4C3 4C4
NULL 5C2 5C3 5C4 5C5
NULL 6C2 6C3 6C4 6C5
NULL 7C2 7C3 7C4 7C5
NULL 8C2 8C3 8C4 8C5
NULL 9C2 9C3 9C4 9C5



并且max函数成功了,但它似乎只检查第一列中没有null的行。



I然后尝试了第二列中的所有空值:

 C2 C3 C4 C5 
---------- ------- --- ---------- ---------- ----------
1C1 NULL 1C3 1C4 1C5
2C1 NULL 2C3 2C4 2C5
3C1 NULL 3C3 3C4 3C5
4C1 NULL 4C3 4C4 4C5
5C1 NULL 5C3 5C4 5C5
6C1 NULL 6C3 6C4 6C5
7C1 NULL 7C3 7C4 7C5
8C1 NULL 8C3 8C4 8C5
9C1 NULL 9C3 9C4 9C5

并且函数失败。

解决方案

我发现了我的问题。 OfType<串gt; 中导致在第一列中跳过包含null的行(也可能在其他列中)。删除它,一切顺利。


I am executing this SQL statement using the sqlclient method:

select top 10 *
   from wlo_104466_CustomerPriceFile
   order by [UPC Code]

Which seems to be working correctly.

However, when I to get the maximum width for each column using the following code:

List<int> maximumLengthForColumns = 
    Enumerable.Range(0, dataTable.Columns.Count)
    .Select(col => dataTable.AsEnumerable()
        .Select(row => row[col]).OfType<string>()
        .Max(val => val.Length)).ToList();

I get an exception: "Sequence contains no elements.

However, I can run the following code on the same datable results from the query:

foreach(DataRow row in dataTable.Rows)
{
  foreach(var item in row.ItemArray)
  {
    Console.Write(item + ",");
  }
  Console.WriteLine();
}

I can write out every row, field by field.

The next query, which excludes the "top 10" limit but otherwise is the same table:

select *
   from wlo_104466_CustomerPriceFile
   order by [UPC Code]

Generates the maximumLengthForColumns list perfectly.

This is the data generated when I run the first query in Microsoft SQL Server Management Studio:

UPC Code         Supplier Code Part No       Pack Qty        Unit of Measure
---------------- ------------- ------------- --------------- ---------------
NULL             HOB           S228519T-P01  14              CTN
NULL             LIN           ED031252      4SRP            CTN
NULL             LIN           ED031253      3.3SRP          CTN
NULL             LIN           ED031267      10.3CT          CTN
NULL             LIN           ED031269      11.9CT          CTN
NULL             LIN           ED031271      10.3CT          CTN
NULL             LIN           ED031288      3.7TB           CTN
NULL             LIN           ED031289      4.6TB           CTN
NULL             LIN           ED031290      3.5TB           CTN
NULL             LIN           ED031291      3.7TB           CTN

(10 row(s) affected)

I like using the clean look of the Linq code and would like to understand what I'm doing wrong.

Thanks for your help.

What I have tried:

1) iterating through the datatable using a foreach loop which seemed to work.
2) when tracing through the maximumLengthForColumns routine I noticed that the execution line never seems to get past the ".Select(row..." line on the query that throws the exception but does get down to the ".Max(val..." line in the query that does.

NEW
I tried using another table with all nulls in the first column:

C1         C2         C3         C4         C5
---------- ---------- ---------- ---------- ----------
NULL       1C2        1C3        1C4        1C5
NULL       2C2        2C3        2C4        2C5
NULL       3C2        3C3        3C4        3C5
NULL       4C2        4C3        4C4        4C5
NULL       5C2        5C3        5C4        5C5
NULL       6C2        6C3        6C4        6C5
NULL       7C2        7C3        7C4        7C5
NULL       8C2        8C3        8C4        8C5
NULL       9C2        9C3        9C4        9C5


and the max width query failed.

I then added 9 rows with all nulls in the second column:

C2         C3         C4         C5
---------- ---------- ---------- ---------- ----------
NULL       1C2        1C3        1C4        1C5
NULL       2C2        2C3        2C4        2C5
NULL       3C2        3C3        3C4        3C5
NULL       4C2        4C3        4C4        4C5
NULL       5C2        5C3        5C4        5C5
NULL       6C2        6C3        6C4        6C5
NULL       7C2        7C3        7C4        7C5
NULL       8C2        8C3        8C4        8C5
NULL       9C2        9C3        9C4        9C5


and the max function succeeded but it only seemed to check the rows without the null in the first column.

I then tried all nulls in the second column:

C2         C3         C4         C5
---------- ---------- ---------- ---------- ----------
1C1        NULL       1C3        1C4        1C5
2C1        NULL       2C3        2C4        2C5
3C1        NULL       3C3        3C4        3C5
4C1        NULL       4C3        4C4        4C5
5C1        NULL       5C3        5C4        5C5
6C1        NULL       6C3        6C4        6C5
7C1        NULL       7C3        7C4        7C5
8C1        NULL       8C3        8C4        8C5
9C1        NULL       9C3        9C4        9C5

and the function failed.

解决方案

I found my problem. "OfType<string>" was causing skipping of rows with null in the first columns (possibly in other columns as well). Removed it and all is well.


这篇关于返回所有行的数据表最大列宽。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆