0

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

2
  • 1
    Could you clarify what you're trying to achieve? i.e. which entities are involved, what their relationships are and what your output should be? The code is quite confusing... Commented Jul 28, 2012 at 13:50
  • @Spikeh, I thought it would be obvious I am working with a SelectList collection and want to set a Boolean value if the ID of the complete list matches the ID value from a similar list. I'll update the question I suppose. Not sure why the down vote. Commented Jul 28, 2012 at 14:33

2 Answers 2

2
var selectedIds = schedule.Instructors.Select(i => i.Id.ToString()).ToList(); var instructors = (from instructor in _instructorService.GetAllNonGuestInstructors() let value = instructor.Id.ToString() select new SelectListItem() { Text = instructor.User.ToFullName(), Value = value, Selected = selectedIds.Contains(value) }).ToList(); 
Sign up to request clarification or add additional context in comments.

Comments

1
 var allInstructors = _instructorService.GetAllNonGuestInstructors(); if(allInstructors!=null) allInstructors.Select(i => new SelectListItem() { Text = i.User.ToFullName(), Value = i.Id.ToString() }). Zip(schedule.Instructors,(selectedItems,instructor)=> {var item = selectedItems.FirstOrDefault(s => s.Value == instructior.Id.ToString()); if(item!=null) item.Selected=true;}); //Now use allInstructors collection further it will have Selected true according to your conditions. 

I can't understand one thing why selectedItem is removed and then added. Anyways I hope this will help.

1 Comment

I was only removing it from the collection so that I could update that item in the selectInstructors collection. It was sloppy way of just performing the update until I could figure out what you provided!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.