I have a linq query (in Entity Framework Core) that joins two sql server tables, filters the results in the where clause, and then selects a set of columns from the results.
var resultsObj = (from rd in _db.ResData
join ra in _db.ResAvailability on rd.RecNo equals ra.RecNoDate
where ra.TotalPrice < Int32.Parse(resDeals.priceHighEnd) && ra.TotalPrice > Int32.Parse(resDeals.priceLowEnd)
select new
{
Name = rd.Name,
ImageUrl = rd.ImageUrl,
ResortDetails = rd.ResortDetails,
CheckIn = ra.CheckIn,
Address = rd.Address,
TotalPrice = ra.TotalPrice
});
ViewBag.resultSet = resultsObj;
I've created a viewmodel class to store the attributes a row of the query results and another viewmodel class which contains a List object to store all instances of the other viewmodel, essentially functioning as a collection for all of the rows returned from the query:
public class ResortDataJoinObj
{
public ResortData ResData { get; set; }
public ResortAvailability ResAvail { get; set; }
}
public class ResortDealResultsObject
{
public List<ResortDataJoinObj> resultsList { get; set; }
}
Once the List has been created, I need to essentially import it into a view page so that the query results can be stored in a table. I've done this process of obtaining query results before, except with stored procedures where I was able to iterate through the reader object and store the returned row data as necessary. I know that it's standard practice to pass data from the controller to the view using Viewmodels, but I'm still new at using linq queries so I'm not sure how to go about actually extracting data returned from the linq query results and binding it to viewmodels.
How can I go about extracting the data from the linq query results and storing it in the viewmodel classes?