Solved

LINQ JOIN and foreach loop

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

679 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