【发布时间】:2015-11-09 10:54:16
【问题描述】:
我尝试在我的应用程序中多次使用相同的选择查询。 例如我有这个选择语句:
_db.tbl_itembundlecontents
.Select(z => new SingleItemDTO
{
amount = z.amount,
id = z.id,
position = z.position,
contentType = new BasicMaterialDTO
{
id = z.tbl_items.id,
img = z.tbl_items.tbl_material.img,
name = z.tbl_items.tbl_material.name,
namekey = z.tbl_items.tbl_material.namekey,
info = z.tbl_items.tbl_material.info,
weight = z.tbl_items.tbl_material.weight
}
});
但我也有这个:
_db.tbl_itembundle
.Where(x => x.type == 2)
.Select(y => new ItemBundleDTO
{
name = y.name,
namekey = y.namekey.
contents = y.tbl_itembundlecontents
.Select(z => new SingleItemDTO
{
amount = z.amount,
id = z.id,
position = z.position,
contentType = new BasicMaterialDTO
{
id = z.tbl_items.id,
img = z.tbl_items.tbl_material.img,
name = z.tbl_items.tbl_material.name,
namekey = z.tbl_items.tbl_material.namekey,
info = z.tbl_items.tbl_material.info,
weight = z.tbl_items.tbl_material.weight
}
})
});
如您所见,我在两个 linq select 语句中使用完全相同的代码(用于 SingleItemDTO)。有没有办法分离我的第一个选择语句的代码(没有得到 NotSupportedException)并再次重用它?我的第二个查询应该是这样的:
_db.tbl_itembundle
.Where(x => x.type == 2)
.Select(y => new ItemBundleDTO
{
name = y.name,
namekey = y.namekey.
contents = y.tbl_itembundlecontents.ToDTO()
});
【问题讨论】:
标签: c# asp.net-mvc entity-framework linq linq-to-entities