我试图弄清楚如何从C#实体框架中检索自定义对象。
我的输出来自数据库。
我正在尝试将其转换为以下JSON编码格式。
{
DT_RowId: 50-1,
donor: "0111158",
serial_number: "0111158-2030RV-001",
status: "Released",
location:"Finished Goods",
ext_dt: "08/2019",
qty: "1",
new_locations: [
{
"location_id":6,
"location_name":"Post Sterilization Quarantine2"
},
{
"location_id":5,
"location_name":"Post Sterilization Quarantine1"
},
{
"location_id":18,
"location_name":"Material Review Board"
}
]
}
到目前为止,这是我所拥有的...但是我遇到了如何构建new_locations列表的障碍。
var details = from entity in db.GetInventoryMoveDeatils(id, username)
select new
{
DT_RowId = entity.DT_RowId,
donor = entity.donor,
serial_lot = entity.SERIAL_NUMBER,
status = entity.status,
location = entity.location,
ext_dt = entity.ext_dt,
qty = entity.qty
};
return Json(new { data = details }, JsonRequestBehavior.AllowGet);
我不确定要搜索哪些关键字,因为我不确定在LINQ查询中该调用什么。因此,我所有的搜索结果都没有返回任何有用的信息。我愿意研究它,但是在我应该搜索的关键词方面需要一些帮助。
new_locations
数组的数据包含在ALLOWABLE_LOCATION
和ALLOWABLE_LOCATION_SYSID
下的数据库结果中。我可以运行两个单独的LINQ查询,并在DT_RowId
键上将它们加入。有X行,其中允许使用不同的位置。
答案 0 :(得分:1)
您可以这样做:
var details = from entity in db.GetInventoryMoveDeatils(id, username)
group entity by entity.DT_RowId in groupedData
select new
{
DT_RowId = groupedData.key,
donor = groupedData.Select(x => x.donor ).FirstOrDefault(),
serial_lot = groupedData.Select(x => x.SERIAL_NUMBER).FirstOrDefault()
status = groupedData.Select(x => x.status).FirstOrDefault()
location = groupedData.Select(x => x.location).FirstOrDefault()
ext_dt = groupedData.Select(x => x.ext_dt).FirstOrDefault()
qty = groupedData.Select(x => x.qty).FirstOrDefault() ,
new_locations = groupedData.Where(x => x.DT_RowId == groupedData.key).Select( a => new
{
location_id = a.ALLOWABLE_LOCATION_SYSID,
location_name = a.ALLOWABLE_LOCATION
}).ToList()
}