您好我正在写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} –