Camillia
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
2. I have a stored proc and get the result
3. I get the Schedule data from the result
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)
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; }
}
2. I have a stored proc and get the result
var result = CurrentContext.Database.SqlQuery<SearchAssetModelGeneral>("[dbo].[usp_SearchAssets]").ToList();
3. I get the Schedule data from the result
var schedule = result.Select(x => new Schedule()
{
ID = x.ID1,
Sched = x.Sched,
..........
}).ToList();
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();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
as always, thanks :)
Not a problem Camillia, as always glad to help.
ASKER
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
https://www.experts-exchange.com/questions/29129190/How-can-I-populate-this-object.html
ASKER