Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

LINQ JOIN and foreach loop

Posted on 2011-09-29
4
Medium Priority
?
381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

670 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