Reputation: 2175
Hi I am writing linq query to return data from multiple tables. I am able to return data. I am using webapi2 and returning response in json format. For example I want to return data in below format.
{
"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"
}
}
As you see above example there is Logofile object and adminUser. I am puting data to these objects from different tables by using joins. What i tried is I created one class with all properties and i am getting response in the below format,
{
"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"
}
However as you can see first format i want logofile and adminuser objects inside the main object. I want to get data in the first format. This is my query
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();
May i get some idea how can i achieve this? Any help would be appreciated. Thank you.
Upvotes: 0
Views: 189
Reputation: 8370
You can declare properties on the anonymous type that are anonymous types themselves. See the adminUser
property as an example below:
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();
Upvotes: 3