Line of code works but I know it's not correct

I hope someone can help me because I've been at this for 2 days now.

I'll go step by step. I'll make the code brief.

1. I have a model like this

		public class SearchAssetModelGeneral //camilla try
		{
			public int? ScheduleID { get; set; }
			public long UnitID { get; set; } 
                         .....
			//public Schedule Schedule { get; set; }

		}

Open in new window


2. I have a stored proc and get the result

	var result = CurrentContext.Database.SqlQuery<SearchAssetModelGeneral>("[dbo].[usp_SearchAssets]").ToList();

Open in new window


3.  I get the Schedule data from the result
var schedule = result.Select(x => new Schedule()
				{
					ID = x.ID1,
					Sched = x.Sched,
				..........


				}).ToList();

Open in new window


4. I get the Unit data but Unit has a FK to Scheudle so I want to attach the Schedule to it. This works if I get one row but how can I change it to work for more than one row?
I've marked it below.

How can I say..... I have more than one Unit rows and attach this Scheulde row to this Unit row (unit.scheduleId = scheudle.id)

var unitResult = (from a in result
				 select new
					{ 
                                           ID = a.UnitID,
					   ScheduleId = a.ID1,
					  }).ToList().Select( c=> new Unit
						{
		                         	  ID = c.ID,
				        	  ScheduleID = c.ScheduleId,
								          ...............
			                        Schedule = schedule.FirstOrDefault() //*****this isn't correct. Gets the first row only. How can I say I have more than one Unit row and this Scheudle Row goes with this Unit row? Can I add a "where" here to say.. Schedule.Id = Unit.ScheduleId to attach the correct Schedule row to the correct Unit row?
									  //Schedule = new Schedule()
									  //{

									  //}
								  }).AsQueryable();

Open in new window

LVL 8
CamilliaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi Camillia;

In this statement
Schedule = schedule.FirstOrDefault()

Open in new window

the FirstOrDefault method will only return the first element/row in the collection. If you want to get all of them replace that line of code with this
Schedule = schedule.Select(r => r)

Open in new window

and that should bring ball all the rows.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CamilliaAuthor Commented:
Thanks, Fernando. Going to work soon. I'll try it. I'll post back.
CamilliaAuthor Commented:
as always, thanks :)
Fernando SotoRetiredCommented:
Not a problem Camillia, as always glad to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.