【发布时间】:2011-11-22 15:03:28
【问题描述】:
我有以下两个类:
public class Base
{
public int Id { get; set; }
public string PropertyA { get; set; }
}
[NotMapped]
public class Derived : Base
{
public string PropertyB { get; set; }
}
我有以下查询:
var baseQ = from b in db.Bases
let propertyB = SomeCalculation()
select new { Base = b, PropertyB = propertyB };
这是按原样工作的。 我想要的是这样的(伪代码):
List<Derived> list = (from b in db.Bases
let propertyB = SomeCalculation()
select new { Base = b, PropertyB = propertyB }).ToList();
是否可以将选择“向下转换”到派生类,或者我必须为派生类编写一个构造函数,看起来像这样:
public Derived(Base b, string b) { ... }
我的最终解决方案:我将派生类更改为以下内容(因为您甚至不能在对象初始化程序中使用 string.Format):
public class Derived
{
public Base Base { get; set; }
public string PropertyB { get; set; }
public string CalculatedProperty { get { ... } }//For string.Format and other stuff
}
我正在做如下分配:
List<Derived> list = (from b in db.Bases
let propertyB = SomeCalculation()
select new Derived { Base = b, PropertyB = propertyB }).ToList();
【问题讨论】:
标签: c# .net linq linq-to-entities