我有一个像这样的实体框架查询 -
var query = from tbl in db.myTable
select new MyModel
{
id = tbl.id,
start = tbl.start,
end = tbl.end
}
MyModel如下 -
public class MyModel
{
public int id {get; set;}
public Range range {get; set;} // contains start and end property
}
在查询中是否有选择将myTable的属性映射到range.start和range.end?
我发现工作的唯一方法是在模型中添加其他属性,如此 -
public class MyModel
{
public int id {get; set;}
public string _start {get; set;}
public string _end {get; set;}
public Range range {get; set;} // contains start and end property
}
然后在查询完成后运行一些代码,将_start映射到range.start,将_end映射到range.end。
这个问题是我预见到需要将start / end映射到实体的许多自定义子模型中。我需要始终从公共文件导入该函数,并记住在查询运行后运行它。如果可以直接将它们映射到像 -
这样的选择中,那将会更加清晰var query = from tbl in db.myTable
select new MyModel
{
id = tbl.id,
range.start = tbl.start,
range.end = tbl.end
}
或
var query = from tbl in db.myTable
select new MyModel
{
id = tbl.id,
range = new Range(tbl.end, tbl.start),
}
答案 0 :(得分:1)
你的意思是这样吗?
var query = from tbl in db.myTable
select new MyModel
{
id = tbl.id,
range = new Range
{
start = tbl.start,
end = tbl.end
}
}