Reputation: 2225
I have text file having data like below:
FacilityID: 23
FacilityName: ACME Medical Center
Facility Location:
RecordID: 1661
Patient: Kistra Halos
Gender: Female
DOB: 7/20/1955
I have to read that file and convert to ListCollection and filter this ListCollection using lambda or Linq way
I have done the same for inserting data in ListCollection and filter using Linq Check below:
static void Main(string[] args)
{
var PatientList = new List<Patient>();
PatientList.Add(new Patient() { FacilityID = 23, FacilityName = "Schedule23", FacilityLocation = "M23", RecordID = 11, PatientName = "P23", Gender = "F", DOB = "01-07-1987" });
Console.WriteLine("List Filter LINQ Way:");
foreach (var v in from p in PatientList
where p.PatientName == "P25" && p.FacilityName == "Schedule25"
select new { p.PatientName, p.FacilityName })
Console.WriteLine(v.PatientName + " is " + v.FacilityName);
}
Upvotes: 0
Views: 48
Reputation: 145
You could simply take the file in using file reader and use string.split() in order to break it down into its parts.
using System.IO; //For File
//Take in file
string file = File.ReadAllText("C:/path/to/file.txt");
//Split into each facility
string[] facilities = file.Split(" ======================== ");
foreach(var facility in facilities)
{
//Split by line
string[] lines = facility.split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
//Take each line, split into title & data and remove whitespace
PatientList.Add(new Patient() {
FacilityID = int.Parse(lines[0].Split(":")[1].trim()),
FacilityName = lines[1].Split(":")[1].Trim(),
FacilityLocation = lines[2].Split(":")[1].Trim(),
RecordID = int.Parse(lines[3].Split(":")[1].Trim()),
PatientName = lines[4].Split(":")[1].Trim(),
Gender = lines[5].Split(":")[1].Trim(),
DOB = lines[6].Split(":")[1].Trim() });
}
This assumes your data is formatted exactly as in your example and that no mistakes are made in the file.
Upvotes: 1