I'll try explaining what I'm trying to do - Currently I have a model for each table I'm using, and a ViewModel to contain only a part of the table content .
I also add an extra property or two to the ModelView, that comes from another table. My question is, how do I assign the value(I have a function that bring me that property) to the viewModel object ?
The ViewModel :
public class StreetViewModel
{
public Int64 Id { get; set; }
public Int64 CityId { get; set; }
public string Name { get; set; }
public string CityName { get; set; } // THIS IS THE EXTRA PROPERTY
}
My Controller action to assign the object :
public IActionResult GetStreetByName(string streetName)
{
var results = _repository.GetStreetByName(streetName); //Return a list of object of type Street
return Ok(Mapper.Map<IEnumerable<StreetViewModel>>(results)); // convert Street object to STREETVIEWMODEL object and return it.
}
My GetStreetByName
method:
public IEnumerable<Street> GetStreetByName(string streetName)
{
return _context.Street.Where(t => t.Name.Contains(streetName)).ToList();
}
This method will return a list of object type Street
for me, in each one I have CityId
, and I want to also select the CityName
which appears only in the City
table.
I also have a method to get city object by an ID:
public City GetCityNameById(int cityId)
{
return _context.City.Where(t => t.Id == cityId).FirstOrDefault();
}
Where and how can I assign a cityName to each record of Street
?