When you get objects from Session
you don't need to worry about your object being modifed, because when you get the object from Session
a copy is already created for you. So your code in fact is redundant.
Here's some general info on how you copy an objects that are passed by reference.
Objects are assigned by reference, which points to the same object. So when you are assigning one variable to another it doesn't copy the object itself.
You need to do a Copy
of the object by implementing IClonable
interface in your class or creating a method which will return a new object with fields copied from existing object like this (this will not work if you have nested objects, if that's the case you need to do a deep copy: How do you do a deep copy of an object in .NET (C# specifically)?) :
public class ListDevicesByLabelModel
{
public ListDevicesByLabelModel Clone()
{
var newObj = new ListDevicesByLabelModel();
newObj.SomeProperty = SomeProperty;
//assign other properites
return newObj;
}
}
And use it later:
ListDevicesByLabelModel data = (ListDevicesByLabelModel)Session["miData"];
ListDevicesByLabelModel tempdata = data.Clone();
if (filterType == "model" && filterKey != null)
{
tempdata.devices.device = data.devices.device.Where(c => c.model == filterKey).ToList();
}
return Json(tempdata, JsonRequestBehavior.AllowGet);