繁体   English   中英

使用LINQ读取多个文件

[英]Reading multiple files with LINQ

我正在使用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)
{

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM