是否可以创建一个Linq来检索DataReader列的最长字符串值?列数据应转换为字符串,然后返回最长的字符串。
到目前为止,我所知道的是如何使用可枚举的DataTable获得相同的内容(正如我问过here),例如:
string maxString = dt
.AsEnumerable()
.Select(row => row[mycolumn].ToString())
.OrderByDescending(st => st.Length)
.FirstOrDefault();
我已经尝试过像这样为DataReader组合上层解决方案:
var enumerable = reader
.Cast<IDataRecord>();
string max_string = enumerable
.Select(record => record.GetName(col).ToString())
.Aggregate((s, a) => a.Length > s.Length ? a : s);
不幸的是,这不起作用,我得到
序列在中不包含任何元素 System.Linq.Enumerable.Aggregate
错误。预先感谢您的帮助:)
编辑:我正在寻找一种解决方案,而不是直接从DataReader对象将数据加载到Datatable等中。我正在尝试避免“内存不足异常”,因为数据很大。
Power Mouse建议的最新尝试(它返回正确的值,但仅从第1列开始):
for (int col = 0; col < reader.FieldCount; col++)
{
string col_name = reader.GetName(col).ToString();
var enumerable = reader.Cast<IDataRecord>();
string max_string = enumerable.Where(x => enumerable.Max(y => y[col_name].ToString()
.Length) == x[col_name].ToString().Length)
.FirstOrDefault()?[col_name].ToString();
Console.WriteLine("max string of column is : " + max_string);
}
答案 0 :(得分:1)
因此,根据您的原始请求:动态使用DataReader时,需要在特定列中找到最长的文本。 请查看示例
string storedString = String.Empty;
SqlConnection connection = new SqlConnection(this.Connection.ConnectionString);
using (connection)
{
string SQLcommand = "select * FROM (VALUES(1, 'xxx' ), (2, 'really long string xxxxxx'), (3, 'short string'), (4, 'another string')) t (id, fName)";
SqlCommand command = new SqlCommand(SQLcommand, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
storedString = reader.Cast<IDataRecord>()
.Where(w=> w.GetOrdinal("fName").ToString().Length == reader.Cast<IDataRecord>()
.Max(m => m.GetOrdinal("fName")
.ToString().Length))
.Select(s=> s.GetString(1))
.FirstOrDefault();
}
}
Console.WriteLine($"The longest string: {storedString}. charcount= {storedString.Length}");
结果将是: 最长的字符串:真的很长的字符串xxxxxx。 charcount = 25
正如您所解释的,您需要检查多个列:
string storedNameString = String.Empty;
string storedName2String = String.Empty;
SqlConnection connection = new SqlConnection(this.Connection.ConnectionString);
using (connection)
{
string SQLcommand = "select * FROM (VALUES(1, 'xxx', 'dddddd' ), (2, 'really long string xxxxxx','dfghdt'), (3, 'short string', 'anothercolumn long string'), (4, 'another string', 'test')) t (id, fName, fName2)";
SqlCommand command = new SqlCommand(SQLcommand, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string fName = reader.GetString(reader.GetOrdinal("fName")).ToString();
if(fName.Length >= storedNameString.Length)
storedNameString = fName;
string fName2 = reader.GetString(reader.GetOrdinal("fName2")).ToString();
if (fName2.Length >= storedName2String.Length)
storedName2String = fName2;
}
}
Console.WriteLine($"The longest string: {storedNameString}. charcount= {storedNameString.Length}");
Console.WriteLine($"The longest string: {storedName2String}. charcount= {storedName2String.Length}");
答案 1 :(得分:0)
不幸的是,我没有LINQ就解决了我的问题。问题在于,使用DataReader不能像存储DataTable一样简单地遍历行和列,而必须在 reader.Read()方法运行时执行某种相同的逻辑。
因此,我想出的最好的办法是在 .Read()方法运行时将列索引及其字符串值存储到Dictionary中。
这样做,您必须注意字符串的空格和空值。这是我的解决方案,对我有好处:
Dictionary<int, string> col_values = new Dictionary<int, string>();
using (OracleDataReader reader = cmd.ExecuteReader())
{
for (int i = 0; i < reader.FieldCount; i++)
{
//First add indexes to Dictionary
// I add column names here - didn't test for string.Empty !!
col_values.Add(i, string.Empty);
}
//Then read row by row and modify Dictionary - If text is larger than string.Empty
//Dictionary must be .ToArray(), or else you'll have an error for modifying collection
while (reader.Read())
{
foreach (var item in col_values.ToArray())
{
string rdr_text = reader[item.Key].ToString().Trim()??string.Empty;
if (item.Value.Length<rdr_text.Length)
{
col_values[item.Key] = rdr_text;
}
}
}
foreach (var item in col_values)
{
//...And here we have all longest strings stored, for each column...Job done
}
}
对于我的目的,此迭代在4分钟内读取约2.3 mio行和12列。速度不是很快,但至少可以正常工作。如果有人有更好/更快的主意,请提供答案。