0

I am creating two lists of objects. One "new" list, one "old list".

I want to take the value of one property from an object on the new list and set the property on the old list on the matching object the the new value.

//Original Solution
        foreach (RyderQuestion quest in myList)
        {
            //compare it to every question in ryder questions
            foreach (RyderQuestion oldQuestion in _ryderQuestions)
            {
                //if the question ids match, they are the same question, and should have the right selected option
                //selecting the option sets the checkbox of the MultipleChoideQuestionControl
                if (oldQuestion.QuestionID == quest.QuestionID)
                {
                    oldQuestion.SelectedOption = quest.SelectedOption;
                }
            }
        }

I am trying to convert it to LINQ to make it more effecient using joins, but how do i update the value directly?

        var x = from quest in myList
                join oldquest in _ryderQuestions
                on new { quest.QuestionID, quest.ShowOn, quest.QuestionOrder }
            equals new { oldquest.QuestionID, oldquest.ShowOn, oldquest.QuestionOrder }
                select oldquest.SelectedOption = quest.SelectedOption;

This query returns the values to the x list, but I want to actually update the object in the old list instead.

5
  • 1
    You can't do that in LINQ. Commented Jan 30, 2015 at 14:28
  • I think your best bet is what I proposed in another q. stackoverflow.com/a/28226427/779513 Commented Jan 30, 2015 at 14:29
  • If you want to update the old list, I see two options. Keep at least one loop , or build an entirely new "old list" and assign to that object. Commented Jan 30, 2015 at 14:29
  • Why are you joining in more properties in the linq statement then in the for loop? Commented Jan 30, 2015 at 14:31
  • @Magnus Im worried about duplicates, ill go back and fix the for loop one day. Commented Jan 30, 2015 at 14:54

2 Answers 2

3

Linq is for querying, not updating. You can join the two lists to line up the object to update, but you'll still have to loop to make the changes:

var query = from quest in myList
            join oldquest in _ryderQuestions
            on new { quest.QuestionID, quest.ShowOn, quest.QuestionOrder }
        equals new { oldquest.QuestionID, oldquest.ShowOn, oldquest.QuestionOrder }
            select new {oldquest, quest};

foreach(var item in query}
    item.oldquest.SelectedOption = item.quest.SelectedOption
Sign up to request clarification or add additional context in comments.

5 Comments

the variables stay in scope outside of the linq query?
I dont have this available...?
@CalvinSmith No, oldquest and quest are properties of a new anonymous type created by the projection select new {oldquest, quest}
@CalvinSmith What do mean it's not available?
thank you, i didnt notice you seleted the questions, not the values
2

For example:

var x = from quest in myList
            join oldquest in _ryderQuestions
            on new { quest.QuestionID, quest.ShowOn, quest.QuestionOrder }
        equals new { oldquest.QuestionID, oldquest.ShowOn, oldquest.QuestionOrder }

            select new {quest , oldquest};

foreach(var item in x)
{
    item.quest.SelectedOption = item.oldquest.SelectedOption;
}

You mean this?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.