简体   繁体   中英

Reading multiple files with LINQ

I'm using LINQ to read and display result from 3 different csv files. The first file is CustomerInfo. Second is PackagePrice and third is HolidayTrans. I need to display result in the listbox based on startDate. My listBox only displays the first record. Here's my LINQ and for loop:

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));
            }

Please help. Thanks in advance

It might be because break; is the second command in your foreach statement. Therefore, only the first record is being printed because in your next foreach statement theres only one date that matches. You need to have only one foreach statement. In other words remove all this:

break;
}
    //putting logic for getting break on a year every time before it changes
    foreach (var personRecord in myQuery1)
{

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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