I have a model which has inside another model as:
public class DesignListViewModel
{
public IList<DesignViewModel> DesignList { get; set; } = new List<DesignViewModel>();
}
So I want to assign a property inside that DesignViewModel in my controller using LINQ as:
var rModel = await _designService.Get(model); //rModel equals to DesignListViewModel
//Trying to assign column duedate
rModel.DesignList.Select(design => design.DueDate).FirstOrDefault() = new DateTime();
But it throws an error:
The left-hand side of an assignment must be a variable, property or indexer
How can I access that column as a variable to assign the new value?
If you need to assign property/field of some class instance (assuming DesignViewModel
is a class) you need to aquire the instance and specify what you are trying to set. For example:
rModel.DesignList.First().DueDate = new DateTime();
Or via variable:
var first = rModel.DesignList.First();
first.DueDate = new DateTime();
Note that FirstOrDefault
in this case does not make much sense cause for empty list it will result in null
with following code producing NullReferenceException
.