如何在linq查询中嵌套对象?

时间:2017-02-24 05:13:54

标签: c# linq asp.net-mvc-5

您好我正在编写linq查询以从多个表返回数据。我能够返回数据。我正在使用webapi2并以json格式返回响应。 例如,我想以下面的格式返回数据。

{
  "id": 1,
  "name" : "Test",
  "key": "test1",
  "logoFileId": 12,
  "projectOverview": "Sample overview",
  "procedureOverview": "Sample overview",
  "adminUserId": 14,
  "client" : "Test client",
  "created": 12312312342,
  "updated": 23232323233
  "updatedUserId":34,
  "createdUserId": 35,
  "status" : "ENABLED",
  "logoFile" : {
    "id": 23,
    "name" :"a.jpg",
    "url": "http://localhost/uploads/a.jpg"
  }
  "adminUser": {
    "id": 343,
    "name": "Project admin name",
    "username": "project.admin1"
  }
}

如上所示,有Logofile对象和adminUser。我通过使用连接将数据放入不同表中的这些对象。我尝试的是我创建了一个具有所有属性的类,我得到了以下格式的响应,

{
  "id": 1,
  "name" : "Test",
  "key": "test1",
  "logoFileId": 12,
  "projectOverview": "Sample overview",
  "procedureOverview": "Sample overview",
  "adminUserId": 14,
  "client" : "Test client",
  "created": 12312312342,
  "updated": 23232323233
  "updatedUserId":34,
  "createdUserId": 35,
  "status" : "ENABLED",
  "fileid": 23,
  "filename" :"a.jpg",
  "fileurl": "http://localhost/uploads/a.jpg",
  "adminid": 343,
  "adminname": "Project admin name",
  "adminusername": "project.admin1"
}

然而,你可以看到第一种格式我想在主对象中使用logofile和adminuser对象。我想以第一种格式获取数据。 这是我的查询

  obj = (from c in objectDB.NCT_Project
                       join user in objectDB.NCT_UserRegistration on c.adminUserId equals user.User_Id
                       join file in objectDB.NCT_FileUpload on c.logoFileId equals file.upld_ID
                       where c.adminUserId == userId
                       select new returnObject 
                       {
                           projectName=c.projectName,
                           project_overview = c.projectOverview,
                           procedure_overview=c.procedureOverview,
                           adminUserId=c.adminUserId,
                           clientName=c.clientName,
                           created_user_id=c.createdUserId,
                           projectStatus=c.projectStatus,
                           adminName=user.Name,
                           phoneNumber=user.User_MobileNum,
                           userRole=user.User_Role,
                           userStatus=user.User_Status,
                           userName=user.User_Name,
                           logo_file_id=c.logoFileId,
                           fileName= file.fileName,
                           fileType=file.fileType,
                           fileUrl=file.filePath
                       }).ToList();

我可以知道如何实现这一目标?任何帮助,将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:3)

您可以在匿名类型上声明匿名类型的属性。请参阅下面的adminUser属性示例:

  obj = (from c in objectDB.NCT_Project
                       join user in objectDB.NCT_UserRegistration on c.adminUserId equals user.User_Id
                       join file in objectDB.NCT_FileUpload on c.logoFileId equals file.upld_ID
                       where c.adminUserId == userId
                       select new returnObject 
                       {
                           projectName=c.projectName,
                           project_overview = c.projectOverview,
                           procedure_overview=c.procedureOverview,
                           adminUserId=c.adminUserId,
                           clientName=c.clientName,
                           created_user_id=c.createdUserId,
                           projectStatus=c.projectStatus,
                           adminName=user.Name,
                           phoneNumber=user.User_MobileNum,
                           userRole=user.User_Role,
                           userStatus=user.User_Status,
                           userName=user.User_Name,
                           logo_file_id=c.logoFileId,
                           fileName= file.fileName,
                           fileType=file.fileType,
                           fileUrl=file.filePath,
                           adminUser = new 
                           {
                               id = user.Id,
                               name = user.Name,
                               username = user.User_Name
                           }
                       }).ToList();