Input file content example

Valeur debitee;Type;Date
-721.19;opbivpvirl;24/08/2004
-392.06;opbivpvirl;18/01/2006
498.50;krivirv;03/10/2005
-42.18;krivirv;29/08/2010
394.42;krivirv;06/06/2004
145.25;opbivpvirl;11/06/2007
64.78;opbivpvirl;22/01/2003
179.57;opbivpvirl;25/03/2003
-125.27;uepkpzeielvv;05/10/2010
-77.05;krivirv;20/11/2009
455.96;opbivpvirl;03/09/2005
-97.27;uepkpzeielvv;30/08/2006
117.10;opbivpvirl;05/11/2005
-221.26;uepkpzeielvv;07/01/2008

File description

This is a CSV semi coma delimited file.

Here are the fields:

  • Amount: decimal value with a point as a decimal separator
  • Type: text value
  • Date: date field in the format DD/MM/YYYY

Class declaration

A matching class can be done in 2 different ways:

First way:

[CsvFile(true, '"', ';')]
public class Compte
{
[CsvFileField(0, NumberDecimalSeparator = ".")]
public decimal Amount { set; get; }

[CsvFileField(1)]
public string Type { set; get; }

[CsvFileField(2, DateTimeFormat = "dd/MM/yyyy")]
public DateTime Date { set; get; }

public decimal? Debit { get { return this.Amount < 0 ? -this.Amount : (decimal?)null; } }
public decimal? Credit { get { return this.Amount >= 0 ? this.Amount : (decimal?)null; } }
}

Second way:

[CsvFile(true, '"', ';')]
public class Compte
{
[CsvFileField("Valeur debitee", NumberDecimalSeparator = ".")]
public decimal Amount { set; get; }

[CsvFileField("Type")]
public string Type { set; get; }

[CsvFileField("Date", DateTimeFormat = "dd/MM/yyyy")]
public DateTime Date { set; get; }

public decimal? Debit { get { return this.Amount < 0 ? -this.Amount : (decimal?)null; } }
public decimal? Credit { get { return this.Amount >= 0 ? this.Amount : (decimal?)null; } }
}

Query on the file

This query gets the total amount per year:

using (FileQuery<Compte> fileQuery = new FileQuery<Compte>(@"InputTestFiles\Comptes.csv"))
fileQuery.Content.GroupBy(i => i.Date.Year).Select(i => new { Year = i.Key, Total = i.Sum(g => g.Amount) }).ToList().ForEach(i => Console.Console("Total for the year {0}: {1}", i.Year, i.Total));

This query sorts entries based on the date field:

using (FileQuery<Compte> fileQuery = new FileQuery<Compte>(@"InputTestFiles\Comptes.csv"))
fileQuery.Content.OrderByDescending(i => i.Date).ToList();

Result of the first query

Total for the year 2004: -175.44
Total for the year 2006: 2703.84
Total for the year 2005: -834.12
Total for the year 2010: -3252.88
Total for the year 2007: 8012.93
Total for the year 2003: 3000.10
Total for the year 2009: 5008.47
Total for the year 2008: -341.71
Total for the year 2011: 1791.20
Total for the year 2012: 1376.37
Total for the year 2002: 3803.73
Total for the year 2001: -76.67
Total for the year 2000: -1014.95
Total for the year 1999: -32.13

 

Last edited Aug 16, 2011 at 10:53 PM by paillave, version 10

Comments

No comments yet.