I'm trying to build a fairly simple winform application to load similar data from several different systems and then display the data in different views/reports. I'm using Entity Framework for the first time and I'm not sure how these "report" views fit into an ORM framework.
The model is extremely basic, a base abstract class called Trade is implemented as several concrete classes with system-specific data. This much I understand, and I can show all trades in a datagrid displayed as either the base class or the concrete implementations (e.g. SystemATrade ) should the user want to see the additional fields (thanks to this SO question).
public abstract class BaseTrade
{
[Key, Column(Order=0)]
public string SourceSystem { get; set; }
[Key, Column(Order = 1)]
public string TradeID {get; set;}
public DateTime TradeDate { get; set; }
public string Buy_Sell { get; set; }
public decimal Quantity { get; set; }
public decimal Price { get; set; }
}
public partial class SystemATrade : Trade
{
public string Field1 { get; set; }
public string Field2 { get; set; }
}
public partial class SystemBTrade : Trade
{
public string Field3 { get; set; }
public string Field4 { get; set; }
}
public class TradeComplianceContext : DbContext
{
public DbSet<BaseTrade> Trades { get; set; }
public TradeComplianceContext() { }
}
Now I want to add some custom views such as self-join Trades on TradeDate and Quantity to show related trades similar to this:
_context = new TradeComplianceContext();
var query = from t1 in _context.Trades
join t2 in _context.Trades on
new
{
Quantity = t1.Quantity,
TradeDate = t1.TradeDate
} equals
new
{
Quantity = t2.Quantity,
TradeDate = t2.TradeDate
}
where
t1.Buy_Sell.Equals("P") && t2.Buy_Sell.Equals("S")
select new
{
t1.SourceSystem,
t1.TradeDate,
t1.Quantity,
t1.TradeID,
t1.Price,
t1.TraderID,
t1.TraderName,
T2_TradeID = t2.TradeID,
T2_Price = t2.Price,
T2_TraderID = t2.TraderID,
T2_TraderName = t2.TraderName
};
return query.ToList();
That code currently exists in my Business Logic Layer and returns a list to the UI layer that assigns the list to the DataGrid.DataSource. It works but it doesn't seem correct. Is it preferred to create a POCO for the anonymous type embedded in the linq query and move this logic to the DAL? Would that be done with an EDMX file and can that sit in parallel to CodeFirst POCOs?
Secondary: If possible I'd love to do a polymorphic report similar to BaseTrade/SystemATrade and include additional inherited fields if the user would prefer to view the report as a SystemATrade instead of just BaseTrade.