I have a model that links to a DB table with a few non-mapped fields that I need to populate via a join in EF.
Here is my current attempt at this which returns the following error:
The specified type member db_current_value is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
Model = MainTable
composite_mapped_id1
composite_mapped_id2
general_mapped_label
mapped_calucluation
not_mapped_property_current //not_mapped
not_mapped_property_previous //not_mapped
var queryCurrent = from t in _context.MainTable
where t.Data_Period == "04/01/2021"
select
new //Anonymous
{
//CASE STATEMENTS HERE
composite_id1 = t.composite_mapped_id1,
composite_id2 = t.composite_mapped_id2,
general_label = t.general_mapped_label,
not_mapped_property_current = t.mapped_calucluation
};
var queryPrevious = from t in _context.MainTable
where t.Data_Period == "01/01/2021"
select
new //Anonymous
{
composite_id1 = t.composite_mapped_id1,
composite_id2 = t.composite_mapped_id2,
not_mapped_property_previous = t.mapped_calucluation
};
var queryFinal = (from c in queryCurrent
from p in queryPrevious
where c.composite_id1 == p.composite_id1 && c.composite_id2 == p.composite_id2
select c).AsEnumerable()
.Select(x => new MainTable
{
general_label = t.general_mapped_label,
not_mapped_property_current = x.not_mapped_property_current,
not_mapped_property_previous = c.not_mapped_property_previous //NOT SURE HOW TO REFERENCE THIS
}).ToList();
Any suggestions on how to tweak this or perhaps a completely different approach?
UPDATE: I Decided to go with a NonMapped ViewModel to support this. Not sure if it's ideal though:
var current = _context.MainTable.Where(c => c.Data_Period == "01/01/2021").ToList();
var previous = _context.MainTable.Where(p => p.Data_Period == "04/01/2021").ToList();
var queryFinal = (from c in current
from p in previous
where c.composite_id1 == p.composite_id1 &&
c.composite_id2 == p.composite_id2
select
new MainTable_Interface_Model
{
general_label = c.general_mapped_label,
property_current = c.mapped_calucluation,
property_previous = p.mapped_calucluation
}).ToList();
UPDATE #2 First UPDATE was too slow so for now I went with my comfort zone of SQL
_context.Database.SqlQuery<MainTable_Interface_Model>(strSQL).ToList<MainTable_Interface_Model>();