Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Camillia

ASKER

Thanks, Fernando. Going to work soon. I'll try it. I'll post back.
as always, thanks :)
Not a problem Camillia, as always glad to help.
I hate to be doing this knowing you're retired but I have an EDMX question that you might be able to help. If you don't have time , it's ok

https://www.experts-exchange.com/questions/29129190/How-can-I-populate-this-object.html