Solved

LINQ JOIN and foreach loop

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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…

838 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