asawyer
asawyer

Reputation: 17808

Updating the original reference to an object that was returned from a linq query

I would like to do something like this:

[HttpPost]
public JsonResult Submit(Person UpdatedPerson)
{
    //Find the original Person that the model was bound against
    //Update the collection's reference to reflect the changes
    //from the posted model version
    Person original = PersonCollection
                        .SingleOrDefault(p=>p.Id == UpdatedPerson.Id);
    if(original!=null)
    {
        //update the value from PersonCollection
        //doesn't work, of course
        original = UpdatedPerson;
    }
}

I'm currently doing this:

[HttpPost]
public JsonResult Submit(Person UpdatedPerson)
{
    //Find the index into PersonCollection for the original 
    //model bound object, use the index to directly mutate the 
    //PersonCollection collection
    int refIndex = -1;
    for(int i=0;i<PersonCollection.Length;i++)
    {
        if(PersonCollection[i].Id == UpdatedPerson.Id)
        {
            refIndex = i;
            break;
        }
    }
    if(refIndex >= 0)
    {
        PersonCollection[refIndex] = UpdatedPerson;
    }
}

It feels like I'm missing something simple here, it shouldn't be so much trouble to accomplish what I'm after.

What is the best practice way to do this sort of stuff?

Upvotes: 3

Views: 1308

Answers (3)

McKay
McKay

Reputation: 12614

In your first example, you make a variable declaration:

Person original;

That original variable is a reference.

Then you assign it to some item in your list

Then later you make a call like this:

original = UpdatedPerson;

In this call, you're updating the variable reference, not anything from the collection.

What you might want to do is something like this (Presuming Person is a class, not a struct):

original.UpdateValuesToMatch(UpdatedPerson);

but you'd have to create such a method on the person object.

Or in your second method, you could simplify from:

    if(PersonCollection[i].Id == UpdatedPerson.Id)
    {
        refIndex = i;
        break;
    }
}
if(refIndex >= 0)
{
    PersonCollection[refIndex] = UpdatedPerson;
}

to

    if(PersonCollection[i].Id == UpdatedPerson.Id)
    {
        PersonCollection[i] = UpdatedPerson;
        break;
    }
}

Upvotes: 1

dknaack
dknaack

Reputation: 60516

Description

You can use FindIndex

Sample

[HttpPost]
public JsonResult Submit(Person UpdatedPerson)
{
    //Find the index into PersonCollection for the original 
    //model bound object, use the index to directly mutate the 
    //PersonCollection collection
    int refIndex = PersonCollection.FindIndex(x => x.Id == UpdatedPerson.Id);
    if (refIndex != -1)
         PersonCollection[refIndex] = UpdatedPerson;
}

More Information

Upvotes: 1

TehBoyan
TehBoyan

Reputation: 6890

When you do this:

original = UpdatedPerson;

you are just assigning the reference to original, thus original now points to the same object that UpdatedPerson points to. and that is why you are losing the reference value for the object that you have retrieved from the collection.

Try assigning the individual fields instead.

Upvotes: 2

Related Questions