Link to home
Start Free TrialLog in
Avatar of Itgirl16
Itgirl16

asked on

LINQ JOIN and foreach loop

Actually I am trying to join table using LINQ query and than doing select thing and trying to foreach loop of the whole thing  and trying to compare that thing but It is giving me error .

 DataClassesDataContext db = new DataClassesDataContext();
        var activities = from o in db.maintenanceOrders
                         from r in db.rentalUnits
                         from tr in db.tenantRentals
                         where tr.TenantID == Convert.ToInt32(TenantID)
                              && o.RentalUnitID == tr.rentalUnitID                            
                              && o.Scheduled_Date.Date == e.Day.Date
                         select new { o,tr};(How to select two things in LINQ and how to loop though of two things in foreach loop how can I mention that.
 

                       
        foreach (maintenanceOrders o in activities)
        {
            HyperLink link = new HyperLink();
            link.Text = o.Comments;
         
            link.CssClass = "activity";
            //link.NavigateUrl =
            //    Page.ClientScript.GetPostBackClientHyperlink(CalendarLinkButton1,
            //        a.ActivityID.ToString(), false);
            e.Cell.Controls.Add(link);
        }
                       
Avatar of Easwaran Paramasivam
Easwaran Paramasivam
Flag of India image

You have to use join keyword to join the tables. Refer: http://www.dotnetperls.com/join

Your Linq query.ToList() will give the result in list. Then apply foreach on that.

http://bytes.com/topic/c-sharp/answers/792588-linq-tolist-when-querying-multiple-sources

Hello Itgirl16

Try the attached code sample. I guessed at the 2nd join as I wasn't sure how the variable 'r' was being used... So feel free to correct that :)

Thanks,

Richard
DataClassesDataContext db = new DataClassesDataContext();
			var activities = from o in db.maintenanceOrders
							 join e in db.rentalUnits
								on e.Day.Date equals o.Scheduled_Date.Date // I guessed at this line as I wasn't sure what 'r' was///
							 join tr in db.tenantRentals
								on tr.rentalUnitID equals o.RentalUnitID
							 where tr.TenantID == Convert.ToInt32(TenantID)
							 select new { o, tr };

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of yogsoft
yogsoft
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Itgirl16
Itgirl16

ASKER

@yogsoft: thanks it works..............Thanks for your help.........