Solved

LINQ JOIN and foreach loop

Posted on 2011-09-29
4
347 Views
Last Modified: 2012-05-12
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);
        }
                       
0
Comment
Question by:Itgirl16
4 Comments
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36867861
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

0
 
LVL 9

Expert Comment

by:richard_hughes
ID: 36890915
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

0
 
LVL 2

Accepted Solution

by:
yogsoft earned 500 total points
ID: 36892412
Here is the sample to select multiple values and looping through it....(through if r is not required you can remove it)

you can either use ForEach extension or normal forach loop with having "var" data type for activity.

Note: You can use complex objects as well in place of arrays in your application.


 
var maintenanceOrders = new []{1,2,3 };
            var rentalUnits = new[] { 1.5, 2.7, 3.6 };
            var tenantRentals = new double[] { 3.22, 5.32, 2.44 };
            //DataClassesDataContext db = new DataClassesDataContext();
            var activities = (from o in maintenanceOrders
                             from r in rentalUnits
                             from tr in tenantRentals
                             //where tr.TenantID == Convert.ToInt32(1)
                             //     && o.RentalUnitID == tr.rentalUnitID
                             //     && o.Scheduled_Date.Date == e.Day.Date
                             select new { o, tr }).ToList();

            activities.ForEach(a =>
            {
                Console.WriteLine(a.o);
                Console.WriteLine(a.tr);
            }
            );
            
            /******** OR ************/
            
            foreach (var activity in activities)
            {
                Console.WriteLine(activity.o);
                Console.WriteLine(activity.tr);
            }

Open in new window

0
 

Author Comment

by:Itgirl16
ID: 36894484
@yogsoft: thanks it works..............Thanks for your help.........
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now