I have the following two classes:
public class Base
{
public int Id { get; set; }
public string PropertyA { get; set; }
}
[NotMapped]
public class Derived : Base
{
public string PropertyB { get; set; }
}
And I have the following Query:
var baseQ = from b in db.Bases
let propertyB = SomeCalculation()
select new { Base = b, PropertyB = propertyB };
This works as is. What i want is something like this (Pseudo-Code):
List<Derived> list = (from b in db.Bases
let propertyB = SomeCalculation()
select new { Base = b, PropertyB = propertyB }).ToList();
Is it possible to "Down-Cast" the selection to a derived class, or do I have to write a Constructor for the Derived class which looks something like this:
public Derived(Base b, string b) { ... }
My final solution: I changed the Derived Class to the following (since you even can't use a string.Format in the object initializer):
public class Derived
{
public Base Base { get; set; }
public string PropertyB { get; set; }
public string CalculatedProperty { get { ... } }//For string.Format and other stuff
}
And I'm doing the assignement as follows:
List<Derived> list = (from b in db.Bases
let propertyB = SomeCalculation()
select new Derived { Base = b, PropertyB = propertyB }).ToList();