Is it possible to convert this code into one LINQ query. I could have sworn I had done something like this before, but I cannot figure out where I might that code might be. I want to perform a sub query and modify the values of the selected items if one list has values from another.
var selectInstructors = _instructorService.GetAllNonGuestInstructors()
.Select(i => new SelectListItem()
{
Text = i.User.ToFullName(),
Value = i.Id.ToString()
}).ToList();
var selectedItems = schedule.Instructors
.Select(instructior1 => selectInstructors.FirstOrDefault(s => s.Value == instructior1.Id.ToString()))
.Where(selectedItem => selectedItem != null);
foreach (var selectedItem in selectedItems )
{
selectInstructors.Remove(selectedItem);
selectedItem.Selected = true;
selectInstructors.Add(selectedItem);
}
So let’s assume in the selectInstructors list I have these values:
John Smith, 1
Jane Doe, 2
Dave Ritter, 3
(before we iterate the persisted instructors the selected Boolean value is a default of false)
The schedule.Instructors class has the persisted list of instructors for that schedule:
John Smith, 1
Dave Ritter, 3
Now, what I would like to do is set any of the Selected properties in selectInstructors where the value is equal to schedule.Instructors
1 Answer