0
嗨我正在寫LINQ查詢來從多個表中獲取數據。我想要獲取如下的對象數組。如何從linq返回數組?
processobject retObj = new processobject();
retObj =(from c in entityObject.NCT_Process
join user in entityObject.NCT_UserRegistration on c.createdUserId equals user.User_Id
join file in entityObject.NCT_FileUpload on c.iconfileId equals file.upld_ID
join templObj in entityObject.NCT_Templates on c.ID equals templObj.processId
where c.processid == "10"
select new processobject
{
id = c.ID,
code = c.code,
flochartContent = c.flowchartContent,
//arrayofTemplates array i want to return
}
);
public class processobject
{
public templatesObject[] arrayofTemplates { get; set; }
public int id { get; set; }
public string flochartContent { get; set; }
public string code { get; set; }
}
在上面的查詢中我寫了條件,其中c.processid == "10"
。所以在NCT_Templates
中存在多個記錄processid
10.我想返回包含所有這些記錄的數組。我在我的自定義類中創建瞭如下所示的數組。 我有這個疑問
processobject retObj = new processobject();
retObj = (from c in entityObject.NCT_Process
join user in entityObject.NCT_UserRegistration on c.createdUserId equals user.User_Id
join file in entityObject.NCT_FileUpload on c.iconfileId equals file.upld_ID
into filesObjFirst
from wt1 in filesObjFirst.DefaultIfEmpty()
join templObj in entityObject.NCT_Templates on c.ID equals templObj.processId
into filesObj
from wt in filesObj.DefaultIfEmpty()
where c.ID == dbObject.ID
select new processobject
{
id = c.ID,
code = c.code,
flochartContent = c.flowchartContent,
name = c.name,
parentId = c.parentId,
projectId = c.projectId,
objectives = c.objectives,
displayOrder = c.dispalyOrder,
iconFileId = c.iconfileId,
level = c.level,
iconFileobj = new iconFile
{
id = wt1.upld_ID,
name = wt1.fileName,
url = wt1.filePath
},
description = c.description,
startCriteria = c.startCriteria,
endCriteria = c.endCriteria,
reporting = c.reporting,
output = c.output,
kpi = c.kpi,
procedureHistory = c.procedureHistory,
role = c.role,
duration = c.duration,
owner = c.owner,
visibility = true,
createdUserId = c.createdUserId,
}).FirstOrDefault();
我有這樣的一個多個查詢
templatesObject[] templatesobject = (from c in entityObject.NCT_Templates
where c.processId == dbObject.ID
join file in entityObject.NCT_FileUpload on c.templateFileId equals file.upld_ID
into filesObjFirst
from wt1 in filesObjFirst.DefaultIfEmpty()
select new templatesObject
{
id = c.id,
title = c.title,
version = c.version,
visible = c.visibility,
filesObj = new iconFileTemplate()
{
id = wt1.upld_ID,
url = wt1.filePath,
name = wt1.fileName
}
}).ToArray();
裏面arrayofTemplates
我想存儲的NCT_Templates
記錄。 任何幫助,將不勝感激。謝謝。
謝謝。我編輯了我的問題。我不知道如何將值分配給屬性。我已經在上面發佈了示例對象。 –
@NiranjanGodbole更新:),請記住,如果數組返回多個數據集您需要使用ICollection如上 – Valkyrie
感謝您的時間。在我的情況下,templateFileId將有多行。我如何分配值給templateFileId的屬性。例如,我寫了id = c.id,title = c.title,version = c.version所以像這樣我怎麼能給url,名字等賦值 –