Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在DataTable对象中,如何获取列列表及其最小/最大字符串.Length?_C#_Sql Server_Linq - Fatal编程技术网

C# 在DataTable对象中,如何获取列列表及其最小/最大字符串.Length?

C# 在DataTable对象中,如何获取列列表及其最小/最大字符串.Length?,c#,sql-server,linq,C#,Sql Server,Linq,当我尝试SqlBulkCopy时,我得到了一个字符串截断错误,我需要调试哪个字段导致了它 到目前为止,我有这个。我可以获取列名,但不知道如何获取它们的最小/最大string.length var columnNames = from columns in table.Columns.Cast<DataColumn>() select columns.ColumnName; var columnNames=from table.columns.Ca

当我尝试
SqlBulkCopy
时,我得到了一个字符串截断错误,我需要调试哪个字段导致了它

到目前为止,我有这个。我可以获取列名,但不知道如何获取它们的最小/最大string.length

var columnNames = from columns in table.Columns.Cast<DataColumn>()
                  select columns.ColumnName;
var columnNames=from table.columns.Cast()中的列
选择columns.ColumnName;

我不知道林克是否有可能。如果不是这样,我愿意将其转储到SQL server中,并在那里执行分析。

使用LINQ对此没有多大意义,但要继续:

var columnNames = from column in table.Columns.Cast<DataColumn>()
                  select new { column.ColumnName, column.MaxLength };
var columnNames=from table.Columns.Cast()中的列
选择新{column.ColumnName,column.MaxLength};

如果需要更多信息,还可以获取模式本身。这只需要
ExecuteReader
一些命令(
select*fromtable
)并在该数据读取器上调用
GetSchemaTable
。这将产生一个表,其中包含关于每一列的更多信息(每列一行)。

使用LINQ来实现这一点并没有多大意义,但接下来:

var columnNames = from column in table.Columns.Cast<DataColumn>()
                  select new { column.ColumnName, column.MaxLength };
var columnNames=from table.Columns.Cast()中的列
选择新{column.ColumnName,column.MaxLength};

如果需要更多信息,还可以获取模式本身。这只需要
ExecuteReader
一些命令(
select*fromtable
)并在该数据读取器上调用
GetSchemaTable
。这将产生一个表,其中包含关于每列的更多信息(每列一行)。

要获得存储在列中的最长字符串的长度,类似这样的操作应该可以:

var columns = 
    from DataColumn column in table.Columns
    where column.DataType == typeof(string)
    select new
    {
        column.ColumnName,
        MaxLength = 
        (
            from DataRow row in table.Rows 
            where !row.IsNull(column) 
            select ((string)row[column]).Length
        ).Max()
    };
或者,在lambda语法中:

var columns = table.Columns.Cast<DataColumn>()
    .Where(column => column.DataType == typeof(string))
    .Select(column => new 
    { 
        column.ColumnName, 
        MaxLength = table.Rows.Cast<DataRow>()
            .Where(row => !row.IsNull(column))
            .Select(row => ((string)row[column]).Length)
            .Max() 
    });
var columns = table.Rows.Cast<DataRow>()
    .SelectMany(row => table.Columns.Cast<DataColumn>(), (row, column) => new { row, column })
    .Where(pair => pair.column.DataType == typeof(string) && !pair.row.IsNull(pair.column))
    .GroupBy(pair => pair.column.ColumnName, (key, items) => new 
    { 
        ColumnName = key, 
        MaxLength = items.Max(x => ((string)x.row[x.column]).Length) 
    });
在lambda语法中:

var columns = table.Columns.Cast<DataColumn>()
    .Where(column => column.DataType == typeof(string))
    .Select(column => new 
    { 
        column.ColumnName, 
        MaxLength = table.Rows.Cast<DataRow>()
            .Where(row => !row.IsNull(column))
            .Select(row => ((string)row[column]).Length)
            .Max() 
    });
var columns = table.Rows.Cast<DataRow>()
    .SelectMany(row => table.Columns.Cast<DataColumn>(), (row, column) => new { row, column })
    .Where(pair => pair.column.DataType == typeof(string) && !pair.row.IsNull(pair.column))
    .GroupBy(pair => pair.column.ColumnName, (key, items) => new 
    { 
        ColumnName = key, 
        MaxLength = items.Max(x => ((string)x.row[x.column]).Length) 
    });
var columns=table.Rows.Cast()
.SelectMany(row=>table.Columns.Cast(),(row,column)=>new{row,column})
.Where(pair=>pair.column.DataType==typeof(string)和&!pair.row.IsNull(pair.column))
.GroupBy(pair=>pair.column.ColumnName,(键,项)=>new
{ 
ColumnName=key,
MaxLength=items.Max(x=>((字符串)x.row[x.column]).Length)
});

要获取存储在列中的最长字符串的长度,应该使用以下类似方法:

var columns = 
    from DataColumn column in table.Columns
    where column.DataType == typeof(string)
    select new
    {
        column.ColumnName,
        MaxLength = 
        (
            from DataRow row in table.Rows 
            where !row.IsNull(column) 
            select ((string)row[column]).Length
        ).Max()
    };
或者,在lambda语法中:

var columns = table.Columns.Cast<DataColumn>()
    .Where(column => column.DataType == typeof(string))
    .Select(column => new 
    { 
        column.ColumnName, 
        MaxLength = table.Rows.Cast<DataRow>()
            .Where(row => !row.IsNull(column))
            .Select(row => ((string)row[column]).Length)
            .Max() 
    });
var columns = table.Rows.Cast<DataRow>()
    .SelectMany(row => table.Columns.Cast<DataColumn>(), (row, column) => new { row, column })
    .Where(pair => pair.column.DataType == typeof(string) && !pair.row.IsNull(pair.column))
    .GroupBy(pair => pair.column.ColumnName, (key, items) => new 
    { 
        ColumnName = key, 
        MaxLength = items.Max(x => ((string)x.row[x.column]).Length) 
    });
在lambda语法中:

var columns = table.Columns.Cast<DataColumn>()
    .Where(column => column.DataType == typeof(string))
    .Select(column => new 
    { 
        column.ColumnName, 
        MaxLength = table.Rows.Cast<DataRow>()
            .Where(row => !row.IsNull(column))
            .Select(row => ((string)row[column]).Length)
            .Max() 
    });
var columns = table.Rows.Cast<DataRow>()
    .SelectMany(row => table.Columns.Cast<DataColumn>(), (row, column) => new { row, column })
    .Where(pair => pair.column.DataType == typeof(string) && !pair.row.IsNull(pair.column))
    .GroupBy(pair => pair.column.ColumnName, (key, items) => new 
    { 
        ColumnName = key, 
        MaxLength = items.Max(x => ((string)x.row[x.column]).Length) 
    });
var columns=table.Rows.Cast()
.SelectMany(row=>table.Columns.Cast(),(row,column)=>new{row,column})
.Where(pair=>pair.column.DataType==typeof(string)和&!pair.row.IsNull(pair.column))
.GroupBy(pair=>pair.column.ColumnName,(键,项)=>new
{ 
ColumnName=key,
MaxLength=items.Max(x=>((字符串)x.row[x.column]).Length)
});

有些不对劲。。。column.MaxLength始终返回-1。请参阅:@kkk-Ah,您的意思是数据中最长的字符串,而不是列的大小。在这种情况下,只需从表中选择max(columnname)即可。谢谢。我从理查德那里得到了答案。我知道MAX(col)。但我的数据首先存在于数据表中,而不是Sql中。有些地方不对劲。。。column.MaxLength始终返回-1。请参阅:@kkk-Ah,您的意思是数据中最长的字符串,而不是列的大小。在这种情况下,只需从表中选择max(columnname)即可。谢谢。我从理查德那里得到了答案。我知道MAX(col)。但我的数据首先存在于DataTable中,而不是Sql中。您是在查找定义的列的最大长度(可能未设置),还是查找存储在列中的最长字符串的长度?@RichardDeeming后者:存储在列中的最长字符串。您是在查找定义的列的最大长度,哪一个可能没有设置,或者列中存储的最长字符串的长度?@RichardDeeming后者:列中存储的最长字符串。太好了!谢谢我甚至不知道你能做嵌套LINQ。干杯。@kk请注意,这将分别对每列的整个数据表进行迭代。如果您只是偶尔这样做一次,这不是问题,但是如果这是您打算经常运行的某个进程(或关心延迟和CPU使用),您可能希望避免这种情况。@Luaan:我添加了一个选项,可以先枚举行。太好了!谢谢我甚至不知道你能做嵌套LINQ。干杯。@kk请注意,这将分别对每列的整个数据表进行迭代。如果您只是偶尔这样做一次,这不是问题,但是如果这是您打算经常运行的某个进程(或关心延迟和CPU使用情况),您可能希望避免这种情况。@Luaan:我添加了一个选项以首先枚举行。