I am working on an application that generates a "report" view based upon options the user selects in a form. As I am "dynamically' generating the necessary database query based upon user input, I have opted for using raw SQL as opposed to a LINQ query.
[HttpPost]
public ActionResult GeneratedReport(String StartDate, String EndDate, String EquipmentID, String LocationID, String UserID )
{
String sqlQuery = String.Format("SELECT * from Records WHERE RecordDate BETWEEN '{0}' and '{1}'", StartDate, EndDate);
GeneratedReportViewModel vm = new GeneratedReportViewModel();
List<Record> records;
bool userFilter = (UserID == "") ? false : true;
bool locationFilter = (LocationID == "") ? false : true;
bool equipmentFilter = (EquipmentID == "") ? false : true;
if(userFilter) {
sqlQuery += " AND UserID = " + UserID;
} if(locationFilter) {
sqlQuery += " AND LocationID = " + LocationID;
} if(equipmentFilter) {
sqlQuery += " AND EquipmentID = " + EquipmentID;
}
records = dbContext.Database.SqlQuery<Record>(sqlQuery).ToList();
vm.FilteredRecords = records;
return View("GeneratedReport", vm);
}
}
The definition for the Entity generated Record class:
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
public partial class Record
{
public int RecordID { get; set; }
public DateTime RecordDate { get; set; }
public int EquipmentID { get; set; }
public int ActionTypeID { get; set; }
public int UserID { get; set; }
[StringLength(200)]
public string Notes { get; set; }
public int LocationID { get; set; }
public virtual Action Action { get; set; }
public virtual Equipment Equipment { get; set; }
public virtual Location Location { get; set; }
public virtual User User { get; set; }
}
}
However, whenever the server attempts to load the view associated with GeneratedReport (code follows), all the navigation properties (User, Location, Equipment, Action) of the Record objects are null.
@model IT_Equipment_Log.ViewModels.GeneratedReportViewModel
@{
ViewBag.Title = "GeneratedReport";
}
<table>
<tr><th>Equipment</th><th>User</th><th>Date</th><th>Location</th><th>Action</th><th>Notes</th></tr>
@foreach (var record in @Model.FilteredRecords)
{
<tr><td>@record.Equipment.SerialNumber</td><td>@record.User.FullName</td><td>@record.RecordDate</td><td>@record.Location.DisplayName</td><td>@record.Action.Description</td><td>@record.Notes</td></tr>
}
</table>
I cannot figure out how to make Include() work in this situation, nor an easy way to convert this SQL statement (since it will vary based upon user input) into a LINQ query. Are there any other options available, or how might I resolve this issue?
Thanks much.