简体   繁体   English

分配索引以读取 F# 查询中的 CSV 行

[英]Assign indexes to read CSV rows in F# query

What is the easiest way to add indexes to the read contents of a CSV/TSV file read in with CsvProvider and a query expression?将索引添加到使用CsvProviderquery表达式读入的 CSV/TSV 文件的读取内容的最简单方法是什么?

I have a tab separated file that contains thousands of orders that regularly need to be read, and the relevant orders are the ones that are the most recent not written to a certain database.我有一个制表符分隔的文件,其中包含数千个定期需要读取的订单,相关订单是最近写入某个数据库的订单。 The orders are not indexed and have no timestamps, so I have to cross reference to see which orders have not yet been written to the database.订单未编入索引且没有时间戳,因此我必须交叉引用以查看哪些订单尚未写入数据库。 I would like to index these so I can find the newest order not written to the DB and then select all rows including and after that (the file is written to sequentially by a 3rd party so the newest orders will be the lines furthest down in the file), but I don't see very simple way to do this in a single query expression so far.我想对这些进行索引,以便我可以找到写入数据库的最新订单,然后选择包括之后的所有行(该文件由第 3 方按顺序写入,因此最新订单将是最下方的行)文件),但到目前为止我没有看到在单个查询表达式中执行此操作的非常简单的方法。

let data = new CsvProvider<fileLocation>()
let allOrders = query {
    for row in data.Rows do
    select row (*perhaps something like a "select (index, row)" here?*)
    (*how do I increment the index in the expression?*)
}

How would I index these as such?我将如何索引这些?

You can use Seq.indexed to transform the sequence data.Rows to a sequence of tuples, where first element is the zero-based index and the second element is the row:您可以使用Seq.indexed将序列data.Rows转换为元组序列,其中第一个元素是从零开始的索引,第二个元素是行:

let allOrders = query {
    for index, row in Seq.indexed data.Rows do
    where (index < threshold)
    select row
}

For illustration of how Seq.indexed works:有关Seq.indexed如何工作的说明:

> let xs = ["a"; "b"; "c"; "d"]
> Seq.indexed xs
val it : seq<int * string> = seq [(0, "a"); (1, "b"); (2, "c"); (3, "d")]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM