使用LINQ读取多个文件
本文关键字:文件 读取 LINQ 使用 | 更新日期: 2023-09-27 17:57:44
我使用LINQ读取并显示来自3个不同csv文件的结果。第一个文件是CustomerInfo。第二个是PackagePrice,第三个是HolidayTrans。我需要根据startDate在列表框中显示结果。我的listBox只显示第一条记录。这是我的LINQ和for循环:
string[] myHolidayTransactionFile = File.ReadAllLines(@"c:'temp'HolidayTrans.csv");
//create an undefined variable myQuery1
//to read each line in the HolidayTrans file
var myQuery1 = from myline in myHolidayTransactionFile
let myField = myline.Split(',')
let id = myField[0]
let startDate = myField[1]
let numOfAdults = myField[2]
let numOfKids = myField[3]
where cid == id
orderby DateTime.Parse(startDate) descending
select new
{
cid,
startDate,
numOfAdults,
numOfKids
};
lstInfo.Items.Add(string.Format(formatLine, "Start Date", "End Date", "Adult Amt", "Kid Amt"));
foreach (var personRecord in myQuery1)
{
startTourDate = personRecord.startDate;
break;
}
//putting logic for getting break on a year every time before it changes
foreach (var personRecord in myQuery1)
{
//personRecord StartDate is equal to the Start tour Date which is selected
if (personRecord.startDate == startTourDate)
{
getNumofDaysTwinAdultPrSingleAdultPr(startTourDate, ref numOfDays, ref twoAdultPr, ref oneAdultPr);
//performing calculations for endate adult amt and kid amt
EndDate = Convert.ToDateTime(startTourDate).AddDays(numOfDays);
if (int.Parse(personRecord.numOfAdults) == 2)
{
adultAmt = twoAdultPr * 2;
}
if (int.Parse(personRecord.numOfAdults) == 1)
{
adultAmt = oneAdultPr;
}
if (int.Parse(personRecord.numOfKids) == 2)
{
kidsAmt = kidsPr * 2;
}
if (int.Parse(personRecord.numOfKids) == 1)
{
kidsAmt = kidsPr;
}
if (int.Parse(personRecord.numOfKids) == 0)
{
kidsAmt = 0;
}
// the subtotal is equal to subtotal + adultamt, it continues till the value stored in the starttourdate
//is same as that of ("transition .name") but as soon as start date changes this condition
//gets false and it get transfer to the else part
subtotalA = subtotalA + adultAmt;
subtotalK = subtotalK + kidsAmt;
//displaying output in a listbox
lstInfo.Items.Add(string.Format(formatLine1, startTourDate, EndDate, adultAmt, kidsAmt));
}
请帮忙。提前感谢
这可能是因为break;
是foreach语句中的第二个命令。因此,只打印第一条记录,因为在您的下一个foreach语句中只有一个日期匹配。您只需要有一个foreach语句。换句话说,删除所有这些:
break;
}
//putting logic for getting break on a year every time before it changes
foreach (var personRecord in myQuery1)
{