I am using complicated SQL queries, i have to use SqlQuery … in simple way:
MODEL:
public class C
{
public int ID { get; set; }
[NotMapped]
public float Value { get; set; }
}
CONTROLLER:
IEnumerable<C> results = db.C.SqlQuery(@"SELECT ID, ATAN(-45.01) as Value from C);
return View(results.ToList());
VIEW:
@model IEnumerable<C>
@foreach (var item in Model) {
@Html.DisplayFor(modelItem => item.Value)
}
and the result for item.Value is NULL.
So my question is , how can i print the computed value from SQL Query ?
Thank you for help.
I would conclude from the fact that
Valueis 0 that EF doesn’t map returned columns to properties that are not mapped in the model.What you could try as an alternative is to define a helper type…
Then query into this type and copy the values to your entity afterwards:
(Normally in a LINQ-to-Entities query you cannot project into an entity with
Select. But I believe that theSelectin the example above does not affect the database query and is LINQ-to-Objects in memory, so it should be allowed. I am not sure, though.)Note that the
resultscollection is not attached to and tracked by the context, but I guess you don’t need it anyway for a GET request to render a view.Of course you could create your view directly based on the
CHelperclass as view model and omit the conversion into theCentity.