简化的问题如下:在实体框架中,我正在进行涉及3个表的连接,并返回连接的结果集,其中涉及3个表中的(某些)字段。
var query = (
from t1 in dbCtx.TB_Entity1
from t2 in dbCtx.TB_Entity2
.Where(p => p.someCol == t1.someCol && t.IsActive == true)
.DefaultIfEmpty() //LEFT JOIN
from t3 in dbCtx.TB_Entity3
.Where(q => q.someCol == t2.someCol)
where t1.IsLatest == true
&& (t1.istatus == 2
|| t1.istatus == 3
)
select new {
t1.col100,
t1.col101,
t2.col200,
t2.col201,
t3.col300,
t3.col301
}).OrderByDescending(t1 => t1.ID);
var anonObjList = query.ToList();
因此,在查询结束时,我写了一个投影来选择我想要的字段。
最后,我使用.ToList()运行查询并获取匿名对象列表。
如何修改查询以投影到MyConcreteClass列表
即。我希望能够写出与
类似的东西List<MyConcreteClass> myObjList = query.ToList();
您可以假设我的具体课程类似于
public class MyConcreteClass
{
public string Col100 { get; set; }
public string Col101 { get; set; }
public string Col200 { get; set; }
public string Col201 { get; set; }
public string Col300 { get; set; }
public string Col301 { get; set; }
}
答案 0 :(得分:0)
您只需使用对象初始化程序语法:
new MyConcreteClass
{
Col100 = t1.col100,
Col101 = t1.col101,
Col200 = t2.col200,
Col201 = t2.col201,
Col300 = t3.col300,
Col301 = t3.col301
}