Solved

LINQ JOIN and foreach loop

Posted on 2011-09-29
4
348 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

18 Experts available now in Live!

Get 1:1 Help Now