Rx扩展是否适合读取文件并存储到数据库 [英] Is Rx extensions suitable for reading a file and store to database

查看:62
本文介绍了Rx扩展是否适合读取文件并存储到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用EPPlus读取的Excel文件非常长。对于每一行,我都会测试它是否满足某些条件,如果是,我将其(代表该行的对象)添加到集合中。读取文件后,我将这些对象存储到数据库中。可以同时做两件事吗?我的想法是要有一些对象集合,这些对象将以某种方式被线程使用,从而将对象保存到数据库中。同时,excel阅读器方法将填充集合...这可以使用Rx完成吗,还是有更好的方法?

I have a really long Excel file wich I read using EPPlus. For each line I test if it meets certain criteria and if so I add the line (an object representing the line) to a collection. When the file is read, I store those objects to the database. Would it be possible to do both things at the same time? My idea is to have a collection of objects that somehow would be consumed by thread that would save the objects to the DB. At the same time the excel reader method would populate the collection... Could this be done using Rx or is there a better method?

谢谢。

推荐答案

替代方案-基于对我的第一个注释。

An alternate answer - based on comments to my first.

创建一个返回函数来自EPPlus / Xls的 IEnumberable< Records> -使用 yield return
然后将序列转换为可观察的

Create a function returning an IEnumberable<Records> from EPPlus/Xls - use yield return then convert the seqence to an observable on the threadpool and you've got the Rx way of having a producer/consumer and BlockingCollection.

function IEnumberable<Records> epplusRecords() 
{
  while (...)
     yield return nextRecord;
}
var myRecords = epplusRecords
   .ToObservable(Scheduler.ThreadPool)
   .Where(rec => meetsCritera(rec))
   .Select(rec => newShape(rec))
   .Do(newRec => writeToDb(newRec))
   .ToArray();

这篇关于Rx扩展是否适合读取文件并存储到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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