Input file content example

AA  p0120110101
BZ1 n0220110102
CE  p0320110101
DR  n0420110105
ETs p0520110101
FYd p0620110101
GU  p0720110101
HI  p0820110101
IO  p0920110101
JP  p1020110101
KQf p1120110101
LSf n1220110110
MDg n1320110101
NF  p1420110101
OG  p1520110101
PHg p1620110101
QJ  n1720110101
Rqsen1820110101
SL1 n1920110101
TM12p2020100101
Ucv p2120110101
VXe p2220110101
WCrtp2320080101
XV  p2420110101
YBe p2520110101
ZNern2620110101

File description

This file is a fixed column fields file.

Here are the fields:

  • Field1: Text from character 0 to character  3 (length: 4)
  • Field2: Number from character 4 to character 6 (length: 3). The first character defines if it as a negative (“n”) or positive (“p”) number
  • DateField: Date from character 7 to character 13 (length 8) in the format YYYYMMDD

Class declaration

First, create a class that will represent a deserialized occurrence of the file:

[FlatFile]
public class FlatTable
{
[FlatFileField(0, 4)]
public string Field1 { set; get; }

[FlatFileField(4, 3, NegativeSign = "n", PositiveSign = "p")]
public int Field2 { set; get; }

[FlatFileField(7, 8, DateTimeFormat = "yyyyMMdd")]
public DateTime DateField { set; get; }
}

Get the list of items

Here is an example of a query against the file

using (FileQuery<FlatTable> fileQuery = new FileQuery<FlatTable>(@"input.txt"))
fileQuery.Content.Where(i => (i.Field2 > 12 || i.Field2 <= 0) && i.DateField.Year == 2011).ToList().ForEach(i => Console.WriteLine("{0:d} {1} {2}", i.DateField, i.Field1, i.Field2));

Result

02/01/2011 BZ1 -2
05/01/2011 DR -4
10/01/2011 LSf -12
01/01/2011 MDg -13
01/01/2011 NF 14
01/01/2011 OG 15
01/01/2011 PHg 16
01/01/2011 QJ -17
01/01/2011 Rqse -18
01/01/2011 SL1 -19
01/01/2011 Ucv 21
01/01/2011 VXe 22
01/01/2011 XV 24
01/01/2011 YBe 25
01/01/2011 ZNer -26

Last edited Aug 15, 2011 at 11:55 PM by paillave, version 8

Comments

No comments yet.