Furkan Ekinci
Furkan Ekinci

Reputation: 2652

Passing object to linq result

I get an item from the list to manuplate it.

App.CurrentQuestion = App.AllQuestionList[dataSourceRowIndex];

After manuplations I save new data to database and get back from database into App.CurrentQuestion, so App.CurrentQuestion's list reference breaks. I want to update list and trying to focus selected item by linq

App.AllQuestionList
    .Where(q => q.qID == App.CurrentQuestion.qID)
    .FirstOrDefault() = App.CurrentQuestion;

but I get an error like "The left-hand side of an assignment must be a variable, property or indexer"

I can use this method

for (int i = 0; i < App.AllQuestionList.Count; i++)
{
    if (App.AllQuestionList[i].qID == App.CurrentQuestion.qID)
    {
        App.AllQuestionList[i] = App.CurrentQuestion;
        break;
    }
}

but looking for an alternative method. Or is there any faster method?

Upvotes: 1

Views: 251

Answers (1)

AD.Net
AD.Net

Reputation: 13409

You shouldn't have to do anything, since it's by reference.

App.CurrentQuestion = App.AllQuestionList[dataSourceRowIndex];

Whatever change you make in App.CurrentQuestion should be reflected in the App.AllQuestionList

App.AllQuestionList[App.AllQuestionList.IndexOf(App.AllQuestionList.
    .Where(q => q.qID == App.CurrentQuestion.qID)
    .First())] = App.CurrentQuestion;

Edit: you can just use the IndexOf to find the index of the object you wanted to find by LINQ query

Upvotes: 2

Related Questions