【问题标题】:Entity Framework to custom object that contains a list of another object实体框架到包含另一个对象列表的自定义对象
【发布时间】:2018-06-19 12:31:45
【问题描述】:

我试图弄清楚如何从 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_LOCATIONALLOWABLE_LOCATION_SYSID 下的数据库结果中。我可以运行两个单独的 LINQ 查询并将它们加入 DT_RowId 键。有 X 行具有允许的不同位置。

【问题讨论】:

  • 要获取new_locations,如果表之间有关系,你应该使用Include。如果不是,则编写另一个 linq 查询(您不需要使用 ToList() 获取数据 - IQueryable 应该可以解决问题)并在第一组和第二组之间执行join。现在关于将 new_locations 作为实体的一部分 --- 为此,您可以在 DT_RowId 上应用 GroupBy,因为您有多个记录并选择所需的列以及位置集合。
  • 你在寻找什么价值观?

标签: c# json entity-framework linq


【解决方案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()
        }

【讨论】:

  • new_locations 数组的数据包含在 ALLOWABLE_LOCATION 和 ALLOWABLE_LOCATION_SYSID 下的数据库结果中。我可以运行两个单独的 LINQ 查询并将它们连接到 DT_RowId 键上。有 X 行具有允许的不同位置。
猜你喜欢
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-08
  • 2019-01-04
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
相关资源
最近更新 更多