您好我正在寫linq查詢來從多個表中返回數據。我能夠返回數據。我正在使用webapi2並以json格式返回響應。 例如,我想以下面的格式返回數據。如何在linq查詢中嵌套對象?
{
"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對象和管理用戶。我通過使用連接將數據從不同的表放入這些對象。我的嘗試是我創建一個類的所有屬性,我湊了以下形式迴應,
{
"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和管理用戶對象的第一格式。我想以第一種格式獲取數據。 這是我的查詢
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();
我可以得到一些想法我怎麼能做到這一點?任何幫助,將不勝感激。謝謝。
你'returnObject'類型應該有一個獨立的類型'LogFile'&'AdminUser'的屬性。然後將它們投影到_LINQ_查詢中。 –
謝謝。正如你所說,公共類returnObject {這裏的所有屬性,以及如何可以定義logofile和adminuser} –