utf8_encode
中提供的此解决方案非常有用:
bool find = csvDataOnlyHeader.Rows.Cast<DataRow>().Any(r => r.ItemArray.Any(c => c.ToString().Contains("SN:")));
DataColumn[] columns = csvDataOnlyHeader.Columns.Cast<DataColumn>().ToArray();
bool anyFieldContains = csvDataOnlyHeader.AsEnumerable()
.Any(row => columns.Any(col => row[col].ToString() == "SN:"));
我的代码:
procedure TFDQuery_MyVersion.SetFilterText(const Value: string);
begin
FilterOptions := [TFilterOption.foCaseInsensitive];
inherited SetFilterText(TOldDatabaseSystemToMSSQL.Filter(Value));
end;
我需要什么:
答案 0 :(得分:1)
有Select
的变体可以捕获索引。
var matchingIndexes = csvDataOnlyHeader.Rows.Cast<DataRow>()
.Select((r, index) => new
{
isMatch = r.ItemArray.Any(c => c.ToString().Contains("SN:")),
index
})
.Where(e => e.isMatch)
.Select(e => e.index);
var firstMatch = matchingIndexes.FirstOrDefault();
为了避免firstMatch
0
(意味着第一个匹配是数组的第0个元素)和0
(意味着没有匹配的索引)之间的歧义,你可以投结果为可空的整数:
...
.Select(e => (int?)e.index)
...
或者您可以使用我的CallMeMaybe库来获取Maybe<int>
:
var firstMatch = matchingIndexes.FirstMaybe();
答案 1 :(得分:1)
您应该能够使用IndexOf
类型的DataRowCollection
方法获取行索引,或者像@StriplingWarrior指出lambda表达式的索引参数(如果原始行中的行的索引)可枚举对你来说足够可靠。
var columns = tbl.Columns.Cast<DataColumn>().ToList();
var enumerableRowCollection = tbl.AsEnumerable();
var results = enumerableRowCollection
.Select((row, index) =>
{
var column = columns.FirstOrDefault(col => row[col].ToString() == "PEPSI");
return new
{
Column = column,
ColumnIndex = column != null ? columns.IndexOf(column) : -1,
Row = row,
RowIndex = index
};
})
.Where(x => x.Column != null)
.ToList();
for (var i = 0; i < results.Count(); i++)
{
var result = results[i];
Console.WriteLine($"Result {i}");
Console.WriteLine($"RowIndex: {result.RowIndex}");
Console.WriteLine($"ColumnIndex: {result.ColumnIndex}");
}