예제 #1
0
    public static async Task ImportCsv(long chatId, long userId, string filePath,
                                       string delimiter = ",",
                                       bool hasHeader   = false)
    {
        Log.Information("Loading file {FilePath}", filePath);

        var csvRecords = CsvUtil.ReadCsv <LearnCsv>(filePath, hasHeader: hasHeader, delimiter: delimiter);

        var values = csvRecords.Select(row => {
            var label = row.Label;
            var msg   = row.Message;
            var cols  = new List <object> {
                label, msg, userId, chatId
            };

            // return new { label, msg, fromId, chatId};
            return(cols.AsEnumerable());
        });

        // new LearnData()
        // {
        // Label = row.Label,
        // Message = row.Message,
        // FromId = fromId,
        // ChatId = chatId
        // }

        Log.Information("Inserting {V} row(s).", values.Count());
        var chunkInsert = values.ChunkBy(1000);
        var cols        = new[] { "label", "message", "from_id", "chat_id" };

        foreach (var value in chunkInsert)
        {
            var insert = new Query(tableName)
                         .ExecForMysql()
                         .Insert(cols, value);
            Log.Information("Inserted to {TableName} {Insert} row(s)", tableName, insert);
        }

        await tableName.MysqlDeleteDuplicateRowAsync("message", printSql : true);
    }
예제 #2
0
        static void Main(string[] args)
        {
            var transactions = CsvUtil.ReadCsv("Transactions");
            var customers    = transactions.Select(t => t.Customer).Distinct();
            var offers       = transactions.Select(t => t.Offer).Distinct().ToList();

            offers.Sort();
            var results = new List <OutputData>();

            foreach (var customer in customers)
            {
                var result = new OutputData();
                result.Customer = customer;
                foreach (var offer in offers)
                {
                    int qtd = transactions.Where(t => t.Offer == offer && t.Customer.Equals(customer)).Count();
                    result.GetType().GetProperty("Offer" + offer).SetValue(result, qtd);
                }
                results.Add(result);
            }
            CsvUtil.WriteCsv(results, "DataPrepared");
        }